Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
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 valuesPositioning:
BboxImageandTransformedBboxplace blurred markers at specific coordinatesLayering: 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.
