How to Set Point Size in Scatter Plot Using Matplotlib
To set the point size in a scatter plot in
matplotlib, use the s parameter in the scatter() function. The s value controls the area of each point in square pixels, so larger values make bigger points.Syntax
The basic syntax to set point size in a scatter plot is:
matplotlib.pyplot.scatter(x, y, s=point_size)Here:
xandyare the data coordinates.sis the size of each point in square pixels.
python
plt.scatter(x, y, s=point_size)
Example
This example shows how to create a scatter plot with different point sizes using the s parameter.
python
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [5, 7, 4, 6, 8] point_sizes = [20, 50, 80, 200, 500] # sizes in square pixels plt.scatter(x, y, s=point_sizes, color='blue') plt.title('Scatter Plot with Different Point Sizes') plt.xlabel('X axis') plt.ylabel('Y axis') plt.show()
Output
A scatter plot window showing 5 points with increasing sizes from left to right.
Common Pitfalls
Common mistakes when setting point size in scatter plots include:
- Using
sas a single number when you want different sizes for each point, or vice versa. - Confusing
swith radius;scontrols area, so size changes are squared. - Passing very large or very small values that make points invisible or too big.
Example of wrong and right usage:
python
# Wrong: passing radius instead of area plt.scatter([1,2], [3,4], s=[5,10]) # points will be very small # Right: use area values plt.scatter([1,2], [3,4], s=[25,100]) # points are larger and visible
Quick Reference
| Parameter | Description | Example |
|---|---|---|
| x | X coordinates of points | [1, 2, 3] |
| y | Y coordinates of points | [4, 5, 6] |
| s | Size of points in square pixels | s=100 |
| color | Color of points | color='red' |
Key Takeaways
Use the 's' parameter in plt.scatter() to set point sizes in square pixels.
The 's' value controls the area, not the radius, so size changes are squared.
You can pass a single number or a list of sizes to control each point individually.
Avoid extremely large or small 's' values to keep points visible and clear.
Always label your plot axes and title for better understanding.