matplotlib:重繪前清除散點數據 (matplotlib: clearing the scatter data before redrawing)


問題描述

matplotlib:重繪前清除散點數據 (matplotlib: clearing the scatter data before redrawing)

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

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

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


參考解法

方法 1:

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

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

(by Andrewbpelson)

參考文件

  1. matplotlib: clearing the scatter data before redrawing (CC BY-SA 3.0/4.0)

#scatter-plot #matplotlib






相關問題

matplotlib:重繪前清除散點數據 (matplotlib: clearing the scatter data before redrawing)

使用 matplotlib 保存散點圖動畫 (Saving scatterplot animations with matplotlib)

在 matplotlib 中圍繞散點圖中的數據點繪製平滑多邊形 (draw a smooth polygon around data points in a scatter plot, in matplotlib)

Java:非常簡單的散點圖實用程序 (Java: Really simple scatter plot utility)

如何在多個圖中選擇一個要編輯的圖? (How to select one plot to be edit in multiple plots?)

d3js散點圖自動更新不起作用 (d3js scatter plot auto update doesnt work)

散點圖中的重疊趨勢線,R (Overlapping Trend Lines in scatterplots, R)

DC.JS 散點圖選擇 (DC.JS scatterplot chart selection)

固定散景散點圖中的軸間距(刻度) (Fixing axis spacing (ticks) in Bokeh scatter plots)

如何在顏色條上繪製散點圖? (How to plot scatter plot points on a colorbar?)

如何在散點圖中以均勻間隔更改 x 軸值? (How to change x axis value with even interval in scatter plot?)

如何在散點圖(地理地圖)上標註數據? (How to annotate data on the scatter plot (geo map)?)







留言討論