【问题标题】:How to set a fixed/static size of circle marker on a scatter plot?如何在散点图上设置固定/静态大小的圆形标记?
【发布时间】:2014-12-05 15:25:14
【问题描述】:

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

【问题讨论】:

    标签: matplotlib geometry marker scatter


    【解决方案1】:

    我建议不要使用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()
    

    生成的图如下所示:

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

    这个放大的版本保持了原来的圆圈大小,所以可以看到“连接”。


    如果您想将圆圈更改为透明,patches.Circlealpha 作为参数。只要确保在调用Circle 而不是add_artist 时插入它:

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

    【讨论】:

    • 是否可以设置圆的alpha值,让它有点透明,当它们重叠时,那部分颜色变暗?
    • @Physicist 查看编辑。调用时可以设置圆的alpha。
    猜你喜欢
    • 2015-02-09
    • 2021-04-18
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    相关资源
    最近更新 更多