matplotlib - 如何在散点图上设置圆形标记的固定/静态大小?

标签 matplotlib geometry marker scatter

我想在散点图上绘制一些随机生成的磁盘的位置,并查看这些磁盘是否相互“连接”。为此,我需要设置固定/链接到轴刻度的每个磁盘的半径。's' plt.scatter 中的参数函数使用点,因此大小相对于轴不是固定的。如果我动态放大绘图,散点标记大小在绘图上保持不变,并且不会随轴放大。
如何设置半径以便它们具有确定的值(相对于轴)?

最佳答案

而不是使用 plt.scatter , 我建议使用 patches.Circle 绘制绘图(类似于 this answer )。这些补丁的大小保持固定,以便您可以动态放大以检查“连接”:

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()

生成的图如下所示:

initial plot

使用绘图窗口中的缩放选项,可以获得这样的绘图:

zoom

这个放大版本保留了原始圆圈大小,因此可以看到“连接”。

如果要将圆圈更改为透明,patches.Circle需要一个 alpha作为论据。只要确保你通过调用 Circle 插入它不是 add_artist :
ax.add_artist(Circle(xy=(x, y), 
              radius=r,
              alpha=0.5))

关于matplotlib - 如何在散点图上设置圆形标记的固定/静态大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27319209/

相关文章:

python - 使用 pandas.cut() 并将其设置为数据帧的索引

algorithm - 三边测量算法将 3 个圆定位为尽可能靠近而不重叠

tsql - 降低几何复杂性的最佳方法是什么

algorithm - 最小面积外接矩形包含凸包的最小距离轨迹

javascript - 谷歌地图更新标记位置而不删除它们

java - Eclipse 插件自定义 View 标记数

python - 由边连接的点的动画图

matplotlib - Matplotlib 中相交透明层的正确图例颜色

javascript - Openlayer - 无法在标记上绑定(bind)弹出窗口

python - 在同一图表中绘制 pandas 中的两个数字列,日期在 x 轴上