Data Visualization Articles

Page 40 of 68

How to make several legend keys to the same entry in Matplotlib?

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

In Matplotlib, you can create a legend entry with multiple keys (symbols) representing different lines or data series. This is useful when you want to group related plots under a single legend label. Basic Approach Use the HandlerTuple class to group multiple plot objects under one legend entry ? import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerTuple plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create two different line plots p1, = plt.plot([1, 2.5, 3], 'r-d', label='Red line') p2, = plt.plot([3, 2, 1], 'k-o', label='Black line') # Create legend with both keys for ...

Read More

Removing Horizontal Lines in image (OpenCV, Python, Matplotlib)

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

Removing horizontal lines from images is useful for preprocessing scanned documents or cleaning up images with unwanted line artifacts. We'll use OpenCV's morphological operations to detect and remove these lines while preserving the important content. Understanding the Process The horizontal line removal process involves several key steps ? Convert the image to grayscale and apply thresholding Use horizontal morphological kernels to detect line structures Find contours of the detected lines Remove the lines by drawing over them Apply repair operations to restore nearby content Complete Implementation Here's a complete example that demonstrates horizontal ...

Read More

Specifying the line width of the legend frame in Matplotlib

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

To specify the line width of the legend frame in Matplotlib, we can use the set_linewidth() method. This allows you to customize the appearance of legend lines independently from the actual plot lines. Basic Example Here's how to modify the line width of legend entries ? 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 data points x = np.linspace(-5, 5, 100) y = np.sin(x) # Create plot fig, ax = plt.subplots() ax.plot(x, y, c='r', label='y=sin(x)', linewidth=3.0) # Create legend ...

Read More

How to plot a Pandas multi-index dataFrame with all xticks (Matplotlib)?

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

When working with multi-index DataFrames in Pandas, plotting with proper x-axis tick labels can be challenging. This guide shows how to create readable plots with all x-tick labels displayed correctly using Matplotlib. Understanding Multi-Index DataFrames A multi-index DataFrame has multiple levels of row or column indices. When plotting such data, the default x-axis labels might not display all information clearly ? Step-by-Step Implementation Creating Sample Multi-Index Data First, let's create a multi-index DataFrame with time-series data grouped by year and month ? import numpy as np import matplotlib.pyplot as plt import pandas as ...

Read More

Is it possible to use pyplot without DISPLAY?

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

Yes, it is possible to use matplotlib pyplot without a display by using a non-interactive backend like Agg. This is particularly useful for server environments or headless systems where no GUI display is available. Setting a Non-Interactive Backend Use matplotlib.use('Agg') before importing pyplot to set a non-interactive backend − import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy as np # Set figure properties plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data x = np.linspace(-np.pi, np.pi, 100) # Plot data plt.plot(x, np.sin(x) * x, c='red') # Save figure without displaying ...

Read More

How to draw a line outside of an axis in Matplotlib?

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

When creating matplotlib plots, you may need to draw lines or arrows outside the main plotting area for annotations or visual emphasis. The annotate() method provides a flexible way to add lines outside the axis boundaries. Understanding Coordinate Systems To draw outside the axis, we use xycoords='axes fraction' where coordinates are expressed as fractions of the axis dimensions. Values outside 0-1 range extend beyond the axis boundaries. Basic Example Here's how to draw a horizontal line below the axis ? import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig ...

Read More

How to animate the colorbar in Matplotlib?

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

To animate the colorbar in Matplotlib, you need to update both the image data and colorbar on each frame. This creates dynamic visualizations where the color mapping changes over time. Basic Colorbar Animation Setup First, let's understand the key components needed for animating a colorbar ? import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from mpl_toolkits.axes_grid1 import make_axes_locatable # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create figure and axis fig = plt.figure() ax = fig.add_subplot(111) # Create space for colorbar using divider ...

Read More

How to use multiple font sizes in one label in Python Matplotlib?

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

To use multiple font sizes in one label in Python Matplotlib, you can combine different text elements or use LaTeX formatting with size commands. This allows creating visually appealing labels with varying emphasis. Method 1: Using LaTeX Size Commands The most effective way is using LaTeX size commands like \large, \small, and \huge within a single title ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [8, 4] plt.rcParams["figure.autolayout"] = True x = np.linspace(-5, 5, 100) y = np.cos(x) plt.plot(x, y, 'b-', linewidth=2) # Multiple font sizes in one title ...

Read More

How to put the Origin at the center of the cos curve in a figure in Python Matplotlib?

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

To put the Origin at the center of the cos curve in a figure, we can manipulate the spine positions in Matplotlib. This creates a mathematical coordinate system where both x and y axes intersect at the origin (0, 0). Steps to Center the Origin Set the figure size and adjust the padding between and around the subplots. Create x and y data points using numpy. Set the position of the axes using spines, top, left, right and bottom. Plot x and y data points using plot() method. Set the title of the plot. To display the ...

Read More

How do I specify an arrow-like linestyle in Matplotlib?

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

To draw an arrow-like linestyle in Matplotlib, you can use the quiver() method to create arrows between consecutive data points, or use annotate() for single arrows with specific styling. Method 1: Using quiver() for Arrow Lines The quiver() method creates arrows between consecutive points to form an arrow-like line ? import numpy as np import matplotlib.pyplot as plt # Set figure size plt.figure(figsize=(10, 6)) # Create data points x = np.linspace(-5, 5, 20) y = np.sin(x) # Create arrow-like line using quiver plt.quiver(x[:-1], y[:-1], x[1:]-x[:-1], y[1:]-y[:-1], ...

Read More
Showing 391–400 of 680 articles
« Prev 1 38 39 40 41 42 68 Next »
Advertisements