Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

matplotlib - How to set a fixed/static size of circle marker on a scatter plot?

I want to plot the location of some disks generated randomly on a scatter plot, and see whether the disks are 'connected' to each other. For this, I need to set the radius of each disk fixed/linked to the axis scale.
The 's' parameter in the plt.scatter function uses points, so the size is not fixed relative to the axis. If I dynamically zoom into the plot, the scatter marker size remains constant on the plot and does not scale up with the axis.
How do I set the radius so they have a definite value (relative to the axis)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of using plt.scatter, I suggest using patches.Circle to draw the plot (similar to this answer). These patches remain fixed in size so that you can dynamically zoom in to check for 'connections':

import matplotlib.pyplot as plt
from matplotlib.patches import Circle # for simplified usage, import this patch

# set up some x,y coordinates and radii
x = [1.0, 2.0, 4.0]
y = [1.0, 2.0, 2.0]
r = [1/(2.0**0.5), 1/(2.0**0.5), 0.25]

fig = plt.figure()

# initialize axis, important: set the aspect ratio to equal
ax = fig.add_subplot(111, aspect='equal')

# define axis limits for all patches to show
ax.axis([min(x)-1., max(x)+1., min(y)-1., max(y)+1.])

# loop through all triplets of x-,y-coordinates and radius and
# plot a circle for each:
for x, y, r in zip(x, y, r):
    ax.add_artist(Circle(xy=(x, y), 
                  radius=r))

plt.show()

The plot this generates looks like this:

initial plot

Using the zoom-option from the plot window, one can obtain such a plot:

zoom

This zoomed in version has kept the original circle size so the 'connection' can be seen.


If you want to change the circles to be transparent, patches.Circle takes an alpha as argument. Just make sure you insert it with the call to Circle not add_artist:

ax.add_artist(Circle(xy=(x, y), 
              radius=r,
              alpha=0.5))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...