Articles on Trending Technologies

Technical articles with clear explanations and examples

How to plot a rectangle on a datetime axis using Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 2K+ Views

To plot a rectangle on a datetime axis using Matplotlib, we need to convert datetime objects to numeric values that Matplotlib can handle. This involves using matplotlib.dates module to work with time-based coordinates. Required Steps Set up the figure and subplot Define datetime anchor points for the rectangle Convert datetime objects to numeric format using mdates.date2num() Create a Rectangle patch with datetime coordinates Add the rectangle to the axes using add_patch() method Configure datetime formatting for the x-axis Set appropriate axis limits and display the plot Example Here's how to create a rectangle on ...

Read More

How to remove scientific notation from a Matplotlib log-log plot?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 4K+ Views

When creating log-log plots in Matplotlib, the axes often display values in scientific notation by default. To display regular decimal numbers instead, you can use the ScalarFormatter from matplotlib's ticker module. Basic Approach The key is to apply ScalarFormatter() to both major and minor ticks on the logarithmic axes ? import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as mticker # Create sample data x = np.array([1, 10, 100, 1000, 10000]) y = np.array([2, 20, 200, 2000, 20000]) # Create the plot plt.figure(figsize=(8, 6)) plt.scatter(x, y, c='blue', s=50) # Set log ...

Read More

How to turn off the upper/right axis tick marks in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 1K+ Views

In Matplotlib, by default, tick marks appear on all four sides of the plot. To create cleaner visualizations, you can selectively turn off the upper (top) and right axis tick marks using the tick_params() method. Using tick_params() Method The most straightforward approach is to use tick_params() with specific parameters to control tick visibility ? import numpy as np import matplotlib.pyplot as plt # Create sample data x = np.linspace(-2, 2, 10) y = np.sin(x) # Create the plot plt.figure(figsize=(8, 4)) plt.plot(x, y, marker='o') # Turn off top and right ticks plt.tick_params(axis="both", which="both", top=False, ...

Read More

How to work with images in Bokeh (Python)?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 823 Views

To work with images in Bokeh, you can use the image_url() method to display images from URLs or local files. This method renders images as plot elements that can be positioned and sized within your visualization. Basic Image Display The following example demonstrates how to display an image using Bokeh ? from bokeh.plotting import figure, show, output_file from bokeh.io import curdoc # Configure output to HTML file output_file('image_example.html') # Create a figure with specified ranges p = figure(x_range=(0, 1), y_range=(0, 1), width=600, ...

Read More

How to show legend elements horizontally in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 7K+ Views

In Matplotlib, legends are displayed vertically by default. To arrange legend elements horizontally, use the ncol parameter in the legend() method to specify the number of columns. Basic Horizontal Legend The ncol parameter controls how many columns the legend should have ? import matplotlib.pyplot as plt # Create sample data x = [1, 2, 3, 4] y1 = [1, 4, 2, 3] y2 = [2, 3, 1, 4] y3 = [3, 1, 4, 2] # Plot lines with labels plt.plot(x, y1, label="Series A") plt.plot(x, y2, label="Series B") plt.plot(x, y3, label="Series C") # ...

Read More

How to change the color of a single bar if a condition is true (Matplotlib)?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 6K+ Views

To change the color of a single bar based on a condition in Matplotlib, we can create a list of colors that applies different colors based on our criteria. This technique is useful for highlighting specific data points in bar charts. Steps Set the figure size and adjust the padding between and around the subplots. Initialize a variable for bar width. Create lists of values and corresponding colors based on conditions. Use bar() method to plot bars with conditional colors. To display the figure, use show() method. Example Here's how to highlight bars with ...

Read More

How to annotate each cell of a heatmap in Seaborn?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 2K+ Views

To annotate each cell of a heatmap in Seaborn, we can set annot=True in the heatmap() method. This displays the actual data values inside each cell, making the heatmap more informative and easier to interpret. Basic Heatmap with Annotations Here's how to create a simple annotated heatmap ? import seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create sample data data = pd.DataFrame(np.random.random((5, 5)), ...

Read More

How do you create line segments between two points in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 47K+ Views

To create line segments between two points in Matplotlib, you can use the plot() method to connect coordinates. This technique is useful for drawing geometric shapes, connecting data points, or creating custom visualizations. Basic Line Segment Here's how to create a simple line segment between two points ? import matplotlib.pyplot as plt # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Define two points point1 = [1, 2] point2 = [3, 4] # Extract x and y coordinates x_values = [point1[0], point2[0]] y_values = [point1[1], point2[1]] # ...

Read More

Matplotlib Backend Differences between Agg and Cairo

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 689 Views

Matplotlib offers different backends for rendering graphics, each optimized for specific output formats. The Agg and Cairo backends are two popular choices with distinct capabilities and use cases. Backend Comparison Backend File Types Graphics Type Description Agg PNG Raster High-quality images using Anti-Grain Geometry engine Cairo PNG, PS, PDF, SVG Raster & Vector Versatile output using Cairo graphics library Using Agg Backend The Agg backend is ideal for high-quality raster images. Here's how to use it ? import matplotlib as mpl import ...

Read More

Find the area between two curves plotted in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 2K+ Views

To find the area between two curves in Matplotlib, we use the fill_between() method. This is useful for visualizing the difference between datasets, confidence intervals, or regions of interest between mathematical functions. Basic Example Let's create two curves and fill the area between them ? import matplotlib.pyplot as plt import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.linspace(0, 1, 100) curve1 = x ** 2 # Parabola curve2 = x # Linear function ...

Read More
Showing 4521–4530 of 61,297 articles
« Prev 1 451 452 453 454 455 6130 Next »
Advertisements