How to plot blurred points in Matplotlib?

Matplotlib allows you to create blurred points by combining Gaussian filters and image transformations. This technique is useful for creating artistic effects, heatmap-like visualizations, or emphasizing data points with varying importance.

Basic Approach

The process involves creating a marker, applying a Gaussian filter for blur effect, and positioning it on the plot using BboxImage.

Example

Here's how to create blurred points with varying blur intensities ?

import matplotlib.pyplot as plt
from scipy import ndimage
from matplotlib.image import BboxImage
from matplotlib.transforms import Bbox, TransformedBbox
import numpy as np

plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True

# Step 1: Create a marker and save as image
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot(0.5, 0.5, 'd', ms=200, color='red')
ax1.set_ylim(0, 1)
ax1.set_xlim(0, 1)
plt.axis('off')
fig1.savefig('marker.png', dpi=100, bbox_inches='tight')

# Load the saved marker
marker = plt.imread('marker.png')
plt.close(fig1)

# Step 2: Create blurred points plot
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)

# Generate random positions
x = 8 * np.random.rand(10) + 1
y = 8 * np.random.rand(10) + 1

# Different blur intensities
sigma = np.arange(5, 50, 5)

# Apply blur and position markers
for xi, yi, sigmai in zip(x, y, sigma):
    markerBlur = ndimage.gaussian_filter(marker, sigmai)
    bb = Bbox.from_bounds(xi, yi, 1, 1)
    bb2 = TransformedBbox(bb, ax2.transData)
    bbox_image = BboxImage(bb2, norm=None, origin=None, clip_on=False)
    bbox_image.set_data(markerBlur)
    ax2.add_artist(bbox_image)

ax2.set_xlim(0, 10)
ax2.set_ylim(0, 10)
ax2.set_title('Blurred Points with Varying Intensity')
ax2.set_xlabel('X-axis')
ax2.set_ylabel('Y-axis')

plt.show()

Output

Blurred Points with Varying Intensity X-axis Y-axis 0 2 4 6 8 10 0 2 4 6 8 10

How It Works

The technique involves these key steps:

  • Marker Creation: A diamond marker is plotted and saved as an image file

  • Gaussian Filter: ndimage.gaussian_filter() applies blur with varying sigma values

  • Positioning: BboxImage and TransformedBbox place blurred markers at specific coordinates

  • Layering: Each blurred marker is added as an artist to the axes

Parameters

Parameter Description Effect
sigma Gaussian filter intensity Higher values = more blur
ms Marker size Controls base marker size
dpi Image resolution Affects blur quality

Conclusion

Blurred points in Matplotlib are created by combining Gaussian filters with image positioning techniques. This method is perfect for creating artistic visualizations or emphasizing data points with varying importance levels.

Updated on: 2026-03-26T00:18:28+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements