【问题标题】:Matplotlib multiple scatter plot pickerMatplotlib 多散点图选择器
【发布时间】:2021-08-13 00:10:21
【问题描述】:

我正在尝试关注this demo 处理散点图上的拾取事件。该演示适用于单个散点图。但是,当我添加第二个散点图时,我只能在单击这些点时获得其中一个图的索引。这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand

def pick_scatter_plot():
    # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)

    x, y, c, s = rand(4, 2)

    def onpick3(event):
        ind = event.ind
        print('onpick3 scatter:', ind, x[ind], y[ind])

    fig, ax = plt.subplots()
    #first scatter plot:
    ax.scatter(x, y, 100, c, marker='o', picker=True)
    #second scatted plot
    ax.scatter(x*2, y, 50, c, marker='s', picker=True)  
    fig.canvas.mpl_connect('pick_event', onpick3)    

if __name__ == '__main__':
    pick_scatter_plot()
    plt.show()  

第一组用标记“o”绘制,第二组用标记“s”绘制,如下所示:

但是,当我点击所有四个点时,我只能得到 0 和 1 的索引。这是我一一点击所有 4 个点时的输出:

('onpick3 scatter:', array([1], dtype=int32), array([0.18243891]), array([0.30505569]))
('onpick3 scatter:', array([1], dtype=int32), array([0.18243891]), array([0.30505569]))
('onpick3 scatter:', array([0], dtype=int32), array([0.35977978]), array([0.66748979]))
('onpick3 scatter:', array([0], dtype=int32), array([0.35977978]), array([0.66748979]))

我的问题是如何正确访问两个散点图的点的索引?将所有数据组合成一个散点图对我来说不起作用,因为我需要用不同的标记绘制多组数据,而scatter 不允许使用标记列表。

【问题讨论】:

    标签: python matplotlib data-visualization


    【解决方案1】:

    据我所知,您必须首先确定您单击的是哪个散点图,然后使用它从正确的数组中获取点。我通过标签识别了每个散点,然后,如果是 _collection0,则从数组 xy 中获取点,如果是来自 _collection1,则从 x2、y中获取点>2

    import matplotlib.pyplot as plt
    import numpy as np
    from numpy.random import rand
    
    def pick_scatter_plot():
        # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
    
        x, y, c, s = rand(4, 2)
    
        def onpick3(event):
            ind = event.ind
            label = event.artist.get_label()
            if label == '_collection0':
                print('onpick3 scatter:', label, ind, x[ind], y[ind])
            elif label == '_collection1':
                print('onpick3 scatter:', label, ind, (x*2)[ind], (y*2)[ind])
    
    
        fig, ax = plt.subplots()
        #first scatter plot:
        ax.scatter(x, y, 100, c, marker='o', picker=True)
        #second scatted plot
        ax.scatter(x*2, y, 50, c, marker='s', picker=True)  
        fig.canvas.mpl_connect('pick_event', onpick3)    
    
    if __name__ == '__main__':
        pick_scatter_plot()
        plt.show()
    

    这是点击所有四个点后的输出:

    onpick3 scatter: _collection0 [0] [0.56367835] [0.45595969]
    onpick3 scatter: _collection0 [1] [0.71088259] [0.22692447]
    onpick3 scatter: _collection1 [0] [1.12735669] [0.91191939]
    onpick3 scatter: _collection1 [1] [1.42176517] [0.45384893]
    

    你可以看到所有的组合: 集合0:点0,点1; 集合1:点0,点1

    【讨论】:

    • 谢谢!集合总是从 0 开始编号吗?或者有没有办法为集合指定名称?在更复杂的算法中,可能很难遵循散点图的顺序,因此指定它们的名称以便之后更容易跟踪可能更安全。
    • 哦,看起来event.artist.get_label()实际上返回了散点图中label=参数指定的标签,如果有指定的话。
    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多