在重绘之前清除散点数据

在重绘之前清除散点数据

本文介绍了matplotlib:在重绘之前清除散点数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a scatter plot, over an imshow (map).I want a click event to add a new scatter point, which I have done by scater(newx,newy)). The trouble is, I then want to add the ability to remove points using a pick event. As there is no remove(pickX,PickY) function, I must get the picked Index and remove them from the list, which means I can't create my scatter as above, I must scatter(allx, ally).

因此,最重要的是,我需要一种方法来删除散点图并用新数据重绘它,而又不更改我的imshow的状态.我已经尝试了:只是一次尝试.

So the bottom line is I need a method of removing the scatter plot and redrawing it with new data, without changing the presence of my imshow. I have tried and tried:just one attempt.

 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 和轴:(.任何实现我梦想的方法都非常感谢.安德鲁

much to my suprise, this dispensed with my imshow and axes too :(.Any methods of achieving my dream is much appreciated.Andrew

推荐答案

首先,您应该好好阅读 此处的事件文档.

Firstly, you should have a good read of the events docs here.

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

You can attach a function which gets called whenever the mouse is clicked. If you maintain a list of artists (points in this case) which can be picked, then you can ask if the mouse click event was inside the artists, and call the artist's remove method. If not, you can create a new artist, and add it to the list of clickable points:

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

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

Hope this helps you achieve your dream. :-)

这篇关于matplotlib:在重绘之前清除散点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:20