Skip to content
Advertisement

How to relate size parameter of .scatter() with radius?

I want to draw some circles using `ax3.scatter(x1, y1, s=r1 , facecolors=’none’, edgecolors=’r’), where:

  • x1 and y1 are the coordinates of these circles
  • r1 is the radius of these circles

I thought typing s = r1 I would get the correct radius, but that’s not the case.

How can I fix this?

Advertisement

Answer

If you change the value of ‘r’ (now 5) to your desired radius, it works. This is adapted from the matplotlib.org website, “Scatter Plots With a Legend”. Should be scatter plots with attitude!

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)
    
fig, ax = plt.subplots()
for color in ['tab:blue', 'tab:orange', 'tab:green']:
    r = 5  #radius
    n = 750 #number of circles
    x, y = np.random.rand(2, n)
    #scale = 200.0 * np.random.rand(n)
    scale = 3.14159 * r**2  #CHANGE r
    ax.scatter(x, y, c=color, s=scale, label=color,
               alpha=0.3, edgecolors='none')

ax.legend()
ax.grid(True)

plt.show()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement