1

這裏是我想解決的問題: 我希望能夠交互式地(i)移除散點(灰點),(ii)添加新的散點,通過點擊該圖。在matplotlib中交互添加和移除散點

import matplotlib.pyplot as plt 
import numpy as np 

fig, ax = plt.subplots(figsize=(16,4)) 
a = np.sin(range(100))*np.random.normal(20,10,100) 
b = [ 5, 15, 25, 30, 40, 50, 75, 85] 

aa = plt.plot(range(len(a)),a,color='red') 
bb = plt.scatter(b,a[b],color='grey',s=50) 

enter image description here

+0

這裏您缺少問題描述。你有什麼嘗試,爲什麼它不成功? – ImportanceOfBeingErnest

+1

由於這不是一個代碼編寫服務,因此您可以先查看[事件處理](http://matplotlib.org/users/event_handling.html)頁面。 – ImportanceOfBeingErnest

+0

我覺得我的問題不像在stackoverflow上找到的大多數問題。我認爲你有點苛刻。我一直在閱讀事件處理頁面,但會感謝同行的一些方向。 – themachinist

回答

1

這可能不是優雅,但它的工作原理。左鍵點擊添加點,右鍵點擊刪除點。

import matplotlib.pyplot as plt 
import numpy as np 

fig, ax = plt.subplots(figsize=(16,4)) 
a_input = np.sin(range(100))*np.random.normal(20,10,100) 
b_input = [ 5, 15, 25, 30, 40, 50, 75, 85] 

a = plt.plot(range(len(a_input)),a_input,color='red')[0] 
b = plt.scatter(b_input,a_input[b_input],color='grey',s=50,picker=5) 

def add_or_remove_point(event): 
    global a 
    xydata_a = np.stack(a.get_data(),axis=1) 
    xdata_a = a.get_xdata() 
    ydata_a = a.get_ydata() 
    global b 
    xydata_b = b.get_offsets() 
    xdata_b = b.get_offsets()[:,0] 
    ydata_b = b.get_offsets()[:,1] 

    #click x-value 
    xdata_click = event.xdata 
    #index of nearest x-value in a 
    xdata_nearest_index_a = (np.abs(xdata_a-xdata_click)).argmin() 
    #new scatter point x-value 
    new_xdata_point_b = xdata_a[xdata_nearest_index_a] 
    #new scatter point [x-value, y-value] 
    new_xydata_point_b = xydata_a[new_xdata_point_b,:] 

    if event.button == 1: 
     if new_xdata_point_b not in xdata_b: 

      #insert new scatter point into b 
      new_xydata_b = np.insert(xydata_b,0,new_xydata_point_b,axis=0) 
      #sort b based on x-axis values 
      new_xydata_b = new_xydata_b[np.argsort(new_xydata_b[:,0])] 
      #update b 
      b.set_offsets(new_xydata_b) 
      plt.draw() 
    elif event.button == 3: 
     if new_xdata_point_b in xdata_b: 
      #remove xdata point b 
      new_xydata_b = np.delete(xydata_b,np.where(xdata_b==new_xdata_point_b),axis=0) 
      print(new_xdata_point_b) 
      #update b 
      b.set_offsets(new_xydata_b) 
     plt.draw() 

fig.canvas.mpl_connect('button_press_event',add_or_remove_point) 
+0

非常好,@themachnist!正是我在找什麼。謝謝。 – maurobio