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
Selected Reading
How to get smooth interpolation when using pcolormesh (Matplotlib)?
Pcolormesh in Matplotlib creates pseudocolor plots with rectangular grids. By default, it produces blocky, pixelated output. To achieve smooth interpolation and eliminate harsh edges, you can use the shading="gouraud" parameter.
Default Pcolormesh vs Smooth Interpolation
Let's first see the difference between default shading and Gouraud shading ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
data = np.random.random((5, 5))
x = np.arange(0, 5, 1)
y = np.arange(0, 5, 1)
x, y = np.meshgrid(x, y)
# Create subplots for comparison
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Default shading (flat)
im1 = ax1.pcolormesh(x, y, data, cmap='RdBu')
ax1.set_title('Default Shading (Flat)')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
# Smooth interpolation (gouraud)
im2 = ax2.pcolormesh(x, y, data, cmap='RdBu', shading='gouraud')
ax2.set_title('Smooth Interpolation (Gouraud)')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
plt.tight_layout()
plt.show()
Creating Smooth Pcolormesh
Here's how to create a smooth interpolated pcolormesh plot ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure parameters
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True
# Create sample data with more variation
x = np.linspace(0, 10, 20)
y = np.linspace(0, 8, 15)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y) + np.random.normal(0, 0.1, X.shape)
# Create smooth pcolormesh
plt.pcolormesh(X, Y, Z, cmap='viridis', shading='gouraud')
plt.colorbar(label='Values')
plt.title('Smooth Interpolated Pcolormesh')
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.show()
Key Parameters for Smooth Interpolation
| Parameter | Values | Effect |
|---|---|---|
shading |
'flat' (default) | Blocky, pixelated appearance |
shading |
'gouraud' | Smooth interpolation between points |
cmap |
Any colormap | Color scheme for the plot |
Advanced Example with Function Data
For better visualization of smooth interpolation, let's use a mathematical function ?
import matplotlib.pyplot as plt
import numpy as np
# Create coordinate arrays
x = np.linspace(-3, 3, 30)
y = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, y)
# Create interesting function data
Z = np.exp(-(X**2 + Y**2)) * np.sin(2*X) * np.cos(2*Y)
# Create the smooth plot
plt.figure(figsize=(10, 8))
mesh = plt.pcolormesh(X, Y, Z, cmap='coolwarm', shading='gouraud')
# Add colorbar and labels
plt.colorbar(mesh, shrink=0.8, label='Function Value')
plt.title('Smooth Pcolormesh with Gouraud Shading', fontsize=14)
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.show()
Conclusion
Use shading='gouraud' in pcolormesh to achieve smooth interpolation between data points. This eliminates the blocky appearance of default flat shading and creates visually appealing gradient transitions in your pseudocolor plots.
Advertisements
