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 can I convert from scatter size to data coordinates in Matplotlib?
Converting scatter plot marker sizes to data coordinates in Matplotlib helps you understand the actual data scale represented by marker sizes. This is useful for creating size legends or performing calculations based on visual marker dimensions.
Basic Scatter Plot with Size Array
First, let's create a scatter plot with different marker sizes ?
import numpy as np import matplotlib.pyplot as plt # Set figure parameters plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points X = np.array([[1, 1], [2, 1], [2.5, 1]]) s = np.array([20, 10000, 10000]) # Create scatter plot fig, ax = plt.subplots() scatter = ax.scatter(X[:, 0], X[:, 1], s=s, cmap='plasma', c=s) plt.show()
Converting Scatter Size to Data Coordinates
To convert marker sizes to data coordinates, we need to understand the relationship between scatter size parameter and actual display size ?
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
X = np.array([[1, 1], [2, 1], [2.5, 1]])
s = np.array([20, 10000, 10000])
fig, ax = plt.subplots()
scatter = ax.scatter(X[:, 0], X[:, 1], s=s, cmap='plasma', c=s)
# Get the current axis limits
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Calculate data coordinate width and height
data_width = xlim[1] - xlim[0]
data_height = ylim[1] - ylim[0]
# Get figure size in inches
fig_width, fig_height = fig.get_size_inches()
# Calculate points per data unit
points_per_inch = 72 # matplotlib uses 72 points per inch
data_to_points_x = (fig_width * points_per_inch) / data_width
data_to_points_y = (fig_height * points_per_inch) / data_height
print(f"Data width: {data_width:.2f}")
print(f"Data height: {data_height:.2f}")
print(f"Points per data unit (X): {data_to_points_x:.2f}")
print(f"Points per data unit (Y): {data_to_points_y:.2f}")
# Convert scatter sizes to data coordinates
# Scatter size is in points squared, so take square root and divide by points per unit
size_in_data_coords = np.sqrt(s) / np.mean([data_to_points_x, data_to_points_y])
print(f"Marker sizes in data coordinates: {size_in_data_coords}")
plt.show()
Practical Example with Size Legend
Here's a practical example showing how to create a size legend using the conversion ?
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
x = np.random.randn(50)
y = np.random.randn(50)
sizes = np.random.randint(10, 1000, 50)
fig, ax = plt.subplots(figsize=(8, 6))
scatter = ax.scatter(x, y, s=sizes, alpha=0.6, c=sizes, cmap='viridis')
# Add colorbar for size reference
cbar = plt.colorbar(scatter)
cbar.set_label('Marker Size')
# Create size legend
legend_sizes = [50, 200, 500, 1000]
legend_labels = ['Small', 'Medium', 'Large', 'Extra Large']
# Create legend elements
legend_elements = []
for size, label in zip(legend_sizes, legend_labels):
legend_elements.append(plt.scatter([], [], s=size, alpha=0.6,
color='gray', label=label))
plt.legend(handles=legend_elements, title='Size Legend',
loc='upper left', bbox_to_anchor=(1.1, 1))
plt.title('Scatter Plot with Size Conversion')
plt.xlabel('X coordinates')
plt.ylabel('Y coordinates')
plt.tight_layout()
plt.show()
Key Points
- Scatter size parameter − Represents area in points squared
- Data coordinates − Actual scale of your plot axes
- Conversion formula − sqrt(size) / points_per_data_unit
- Points per inch − Matplotlib uses 72 points per inch
Conclusion
Converting scatter sizes to data coordinates requires understanding the relationship between marker size parameters and actual plot dimensions. Use the conversion formula with figure dimensions and axis limits to create accurate size legends and perform size-based calculations.
