【问题标题】:matplotlib: clearing the scatter data before redrawingmatplotlib:重绘前清除散点数据
【发布时间】:2012-08-12 00:55:31
【问题描述】:

我有一个散点图,位于 imshow(地图)上。 我想要一个点击事件来添加一个新的散点,我已经通过 scater(newx,newy)) 完成了。问题是,然后我想添加使用选择事件删除点的功能。由于没有 remove(pickX,PickY) 函数,我必须获取选中的索引并将它们从列表中删除,这意味着我不能像上面那样创建我的散点图,我必须散点图(allx, ally)。

所以底线是我需要一种删除散点图并用新数据重新绘制它的方法,而不改变我的 imshow 的存在。我已经尝试过: 一次尝试。

 fig = Figure()
 axes = fig.add_subplot(111)
 axes2 = fig.add_subplot(111)
 axes.imshow(map)
 axes2.scatter(allx,ally)
 # and the redraw
 fig.delaxes(axes2)
 axes2 = fig.add_subplot(111)
 axes2.scatter(NewscatterpointsX,NewscatterpointsY,picker=5)
 canvas.draw()

令我惊讶的是,这也省去了我的 imshow 和轴 :(。 任何实现我梦想的方法都非常感谢。 安德鲁

【问题讨论】:

    标签: matplotlib scatter-plot


    【解决方案1】:

    首先,您应该好好阅读events docs here

    您可以附加一个在单击鼠标时调用的函数。如果您维护一个可以选择的艺术家列表(在这种情况下为点),那么您可以询问鼠标单击事件是否在艺术家内部,并调用艺术家的remove 方法。如果没有,您可以创建一个新艺术家,并将其添加到可点击点列表中:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = plt.axes()
    
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    
    pickable_artists = []
    pt, = ax.plot(0.5, 0.5, 'o')  # 5 points tolerance
    pickable_artists.append(pt)
    
    
    def onclick(event):
        if event.inaxes is not None and not hasattr(event, 'already_picked'):
            ax = event.inaxes
    
            remove = [artist for artist in pickable_artists if artist.contains(event)[0]]
    
            if not remove:
                # add a pt
                x, y = ax.transData.inverted().transform_point([event.x, event.y])
                pt, = ax.plot(x, y, 'o', picker=5)
                pickable_artists.append(pt)
            else:
                for artist in remove:
                    artist.remove()
            plt.draw()
    
    
    fig.canvas.mpl_connect('button_release_event', onclick)
    
    plt.show()
    

    希望这可以帮助您实现梦想。 :-)

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      相关资源
      最近更新 更多