交互性(Interactivity)

 1 plt.figure()
 2 data = np.random.rand(10)
 3 plt.plot(data)
 4 #创建点击事件
 5 def onclick(event):
 6     plt.cla()
 7     plt.plot(data)
 8     plt.gca().set_title('Event at pixels {},{} 
and data {},{}'.format(event.x, event.y, event.xdata, event.ydata))
 9 
10 # tell mpl_connect we want to pass a 'button_press_event' into onclick when the event is detected
11 plt.gcf().canvas.mpl_connect('button_press_event', onclick)

1 from random import shuffle
2 origins = ['China', 'Brazil', 'India', 'USA', 'Canada', 'UK', 'Germany', 'Iraq', 'Chile', 'Mexico']
3 
4 shuffle(origins)
5 
6 df = pd.DataFrame({'height': np.random.rand(10),
7                    'weight': np.random.rand(10),
8                    'origin': origins})
9 df

1 plt.figure()
2 # picker=5 means the mouse doesn't have to click directly on an event, but can be up to 5 pixels away
3 plt.scatter(df['height'], df['weight'], picker=5)
4 plt.gca().set_ylabel('Weight')
5 plt.gca().set_xlabel('Height')
1 def onpick(event):
2     origin = df.iloc[event.ind[0]]['origin']
3     plt.gca().set_title('Selected item came from {}'.format(origin))
4 
5 # tell mpl_connect we want to pass a 'pick_event' into onpick when the event is detected
6 plt.gcf().canvas.mpl_connect('pick_event', onpick)

原文地址:https://www.cnblogs.com/zhengzhe/p/8536110.html