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 plot scatter masked points and add a line demarking masked regions in Matplotlib?
To plot scattered masked points and add a line to demark the masked regions, we can use matplotlib's masking capabilities along with the scatter() and plot() methods. This technique is useful for visualizing data that falls within or outside specific boundaries.
Steps
- Set the figure size and adjust the padding between and around the subplots
- Create N, r0, x, y, area, c, r, area1 and area2 data points using NumPy
- Plot x and y data points using
scatter()method with different markers for masked regions - To demark the masked regions, plot the boundary curve using
plot()method - Display the figure using
show()method
Example
Let's create a scatter plot with points masked based on their distance from the origin ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Generate random data
N = 100
r0 = 0.6
x = 0.9 * np.random.rand(N)
y = 0.9 * np.random.rand(N)
area = (20 * np.random.rand(N))**2
c = np.sqrt(area)
# Calculate distance from origin
r = np.sqrt(x ** 2 + y ** 2)
# Create masked arrays based on distance threshold
area1 = np.ma.masked_where(r < r0, area) # Points outside radius
area2 = np.ma.masked_where(r >= r0, area) # Points inside radius
# Plot scattered points with different markers
plt.scatter(x, y, s=area1, marker='^', c=c, label='Outside radius')
plt.scatter(x, y, s=area2, marker='o', c=c, label='Inside radius')
# Draw the boundary line (quarter circle)
theta = np.arange(0, np.pi / 2, 0.01)
plt.plot(r0 * np.cos(theta), r0 * np.sin(theta), 'k-', linewidth=2, label='Boundary')
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter Plot with Masked Regions')
plt.show()
How It Works
The masking process works by:
-
Distance calculation:
r = np.sqrt(x ** 2 + y ** 2)computes the distance of each point from the origin -
Conditional masking:
np.ma.masked_where()creates masked arrays where certain conditions are met - Different markers: Points inside the radius use circles ('o'), while points outside use triangles ('^')
- Boundary visualization: A quarter circle is drawn using parametric equations to show the demarcation line
Key Parameters
| Parameter | Description | Purpose |
|---|---|---|
r0 |
Radius threshold (0.6) | Defines the boundary for masking |
marker |
'^' for triangles, 'o' for circles | Distinguishes masked regions visually |
s |
Size array (area1, area2) | Controls point sizes based on masking |
Conclusion
Using np.ma.masked_where() with different markers effectively visualizes data regions. The boundary line helps clearly separate the masked areas, making the plot both informative and visually appealing.
Advertisements
