如何在散点图上设置圆形标记的固定/静态大小? [英] How to set a fixed/static size of circle marker on a scatter plot?

查看:140
本文介绍了如何在散点图上设置圆形标记的固定/静态大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我建议不要使用此答案) .这些补丁的大小保持不变,因此您可以动态放大以检查连接":

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

生成的图如下:

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

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

此放大版本保留了原始的圆圈大小,因此可以看到连接".

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

如果要将圆更改为透明,则patches.Circlealpha作为参数.只需确保将其插入到调用Circle而不是add_artist:

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

这篇关于如何在散点图上设置圆形标记的固定/静态大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆