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
Plotting a masked surface plot using Python, Numpy and Matplotlib
A masked surface plot allows you to hide or display only specific portions of 3D surface data based on certain conditions. This is useful when you want to exclude invalid data points or highlight specific regions of your surface.
Setting Up the Environment
First, we need to import the required libraries and configure the plot settings ?
import matplotlib.pyplot as plt import numpy as np # Set figure size and layout plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True
Creating the Coordinate Grid
We'll create a coordinate grid using meshgrid to define our surface ?
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create coordinate grids
pi, theta = np.meshgrid(
np.arange(1, 10, 2) * np.pi / 4,
np.arange(1, 10, 2) * np.pi / 4
)
print("Pi grid shape:", pi.shape)
print("Theta grid shape:", theta.shape)
Pi grid shape: (4, 4) Theta grid shape: (4, 4)
Generating Surface Data with Masking
Now we'll create the x, y, z coordinates and apply masking to hide certain data points ?
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
# Create coordinate grids
pi, theta = np.meshgrid(
np.arange(1, 10, 2) * np.pi / 4,
np.arange(1, 10, 2) * np.pi / 4
)
# Generate surface coordinates
x = np.cos(pi) * np.sin(theta)
y = np.sin(pi) * np.sin(theta)
# Apply mask to z-values where x >= 0.01
z = np.ma.masked_where(x >= 0.01, y)
# Create the surface plot
ax.plot_surface(x, y, z, color='red')
plt.show()
How Masking Works
The np.ma.masked_where() function creates a masked array where values meeting the condition are hidden from the plot. In our example:
-
Condition:
x >= 0.01- masks points where x-coordinate is greater than or equal to 0.01 - Result: Only portions of the surface where x < 0.01 are displayed
- Effect: Creates a partial surface with gaps where data is masked
Alternative Masking Examples
You can apply different masking conditions to create various visual effects ?
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4), subplot_kw={'projection': '3d'})
# Create coordinate grids
pi, theta = np.meshgrid(
np.arange(1, 10, 2) * np.pi / 4,
np.arange(1, 10, 2) * np.pi / 4
)
x = np.cos(pi) * np.sin(theta)
y = np.sin(pi) * np.sin(theta)
# Mask based on distance from origin
z1 = np.ma.masked_where(np.sqrt(x**2 + y**2) > 0.8, y)
ax1.plot_surface(x, y, z1, color='blue')
ax1.set_title('Masked by Distance')
# Mask based on y-coordinate
z2 = np.ma.masked_where(y < 0, y)
ax2.plot_surface(x, y, z2, color='green')
ax2.set_title('Masked by Y-coordinate')
plt.tight_layout()
plt.show()
Conclusion
Masked surface plots in Python use np.ma.masked_where() to selectively hide data points based on specified conditions. This technique is valuable for visualizing partial surfaces, excluding outliers, or highlighting specific regions of your 3D data.
