0
0
MatplotlibHow-ToBeginner ยท 3 min read

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:

  • x and y are the data coordinates.
  • s is 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 s as a single number when you want different sizes for each point, or vice versa.
  • Confusing s with radius; s controls 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

ParameterDescriptionExample
xX coordinates of points[1, 2, 3]
yY coordinates of points[4, 5, 6]
sSize of points in square pixelss=100
colorColor of pointscolor='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.