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
Plot scatter points on a 3D projection with varying marker size in Matplotlib
To plot scatter points on a 3D projection with varying marker size in Matplotlib, we need to create 3D coordinates and define marker sizes based on data values. This creates visually appealing scatter plots where point size represents an additional dimension of data.
Steps to Create 3D Scatter Plot with Varying Marker Size
- Set the figure size and adjust the padding between and around the subplots
- Create xs, ys and zs data points using NumPy
- Initialize a variable 's' for varying size of markers
- Create a figure using
figure()method - Add a 3D subplot using
add_subplot()withprojection='3d' - Plot the scatter points using
scatter()method with varying sizes - Display the figure using
show()method
Example
Here's how to create a 3D scatter plot with marker sizes proportional to one of the data dimensions ?
import numpy as np
from matplotlib import pyplot as plt
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Generate random 3D data points
xs = np.random.randint(low=8, high=30, size=35)
ys = np.random.randint(130, 195, 35)
zs = np.random.randint(30, 160, 35)
# Calculate marker sizes based on data values
s = zs / ((ys * 0.01) ** 2)
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Create scatter plot with varying marker sizes and colors
ax.scatter(ys, zs, xs, s=s * 5, c=xs, cmap='copper')
# Add labels for better understanding
ax.set_xlabel('Y values')
ax.set_ylabel('Z values')
ax.set_zlabel('X values')
ax.set_title('3D Scatter Plot with Varying Marker Sizes')
plt.show()
Output
The output will be a 3D scatter plot where points have different sizes based on the calculated 's' values, and colors represent the x-coordinates using the copper colormap.
Customizing Marker Properties
You can further customize the scatter plot by adjusting various parameters ?
import numpy as np
from matplotlib import pyplot as plt
# Generate sample data
np.random.seed(42) # For reproducible results
n_points = 50
xs = np.random.normal(20, 5, n_points)
ys = np.random.normal(160, 15, n_points)
zs = np.random.normal(100, 25, n_points)
# Create different marker sizes
sizes = np.random.uniform(20, 200, n_points)
# Create 3D scatter plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot with custom properties
scatter = ax.scatter(xs, ys, zs, s=sizes, c=zs, cmap='viridis', alpha=0.6, edgecolors='black')
# Add colorbar
plt.colorbar(scatter, shrink=0.5, aspect=10)
# Customize plot
ax.set_xlabel('X values')
ax.set_ylabel('Y values')
ax.set_zlabel('Z values')
ax.set_title('Customized 3D Scatter Plot')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
s |
Marker size (scalar or array) | 50, [20, 30, 40] |
c |
Marker colors | 'red', array of values |
cmap |
Colormap for color mapping | 'viridis', 'copper', 'plasma' |
alpha |
Transparency (0-1) | 0.5, 0.8 |
edgecolors |
Edge color of markers | 'black', 'white' |
Conclusion
Creating 3D scatter plots with varying marker sizes adds an extra dimension to data visualization. Use the s parameter to control marker sizes and combine it with color mapping for comprehensive data representation in three-dimensional space.
