天天看点

matplotlib中,使用Event编码的学习步骤第一步:https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py第二步:https://matplotlib.org/2.0.0/users/artists.html#object-containers第三步:https://matplotlib.org/users/event_handling.html

第一步:https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py

看这个了解大概的matplotlib使用的背景框架:基于函数编程与基于对象编程的区别:

Figure, Axes, Axis, Tick,Canvas之间的关系是什么?

Backend主要包含的FigureCanvas 与 Renderer分别可以表现为画布和画笔,而Artist就是实现画笔与画布之间连接的桥梁?

理解什么是交互式,什么是非交互式,如何将screen置于前段进行显示?

matplotlib中,使用Event编码的学习步骤第一步:https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py第二步:https://matplotlib.org/2.0.0/users/artists.html#object-containers第三步:https://matplotlib.org/users/event_handling.html

第二步:https://matplotlib.org/2.0.0/users/artists.html#object-containers

了解各个Artist之间如何实现互相引用,他们的Property有哪些?如何用get与set函数调用查看这些Property。

以及Container Artist主要包含的attributes有哪些?

对于figure 来说:

fig.axes, fig.images; fig.lines, fig.texts, fig,legends 都是在一个list里面,fig.axes是一个包含container的list,其他的都是primitive的list。fig.patch 是一个背景对象,由于一个figure只有一个背景,所以它不是list

对于Axes来说:

包含的primitive对象和figure一致,只是多出两个xaxis与yaxis对象。

Axes的核心是有很多helper function,辅助axes来构建primitive artist:如plot(),scatter(),bar()等,并且把他们存入到Axes中。可以通过axes.lines, axes.collections, axis.patches等来直接调用。当时要注意:它们都是list,要使用循环来对其中每个对象操作,比如:

fig = plt.figure()

ax = fig.add_subplot(111)

rects = ax.bar(range(10), 20*np.random.rand(10))

drs = []

for rect in rects:

dr = DraggableRectangle(rect)

#DraggableRectangle是一个自定义函数,对bar中每一个柱形进行操作。
           

第三步:https://matplotlib.org/users/event_handling.html

学习event,交互式操作

MouseEvent 'button_press_event' 鼠标点击
'button_release_event' 鼠标释放
'motion_notify_event' 鼠标滑动
'scroll_event' 鼠标滑轮滑动
KeyEvent 'key_press_event' 键盘按下
'key_release_event' 键盘释放
PickEvent  'pick_event' Figure的canvas被选取
LocationEvent 'figure_enter_event' 鼠标进入figure
'figure_leave_event' 鼠标离开figure
'axes_enter_event' 鼠标进入axes
'axes_leave_event' 鼠标离开axes

MouseEvent与KeyEvent从LocationEvent中派生而来,所以三者都具有如下属性:

event.x,event.y 获取像素坐标

event.xdata,event.ydata 获取坐标轴内坐标

event.inaxes 如果鼠标在axes上则返回axes对象实例
           

对于LocationEvent,它还具有如下属性:

event.button 表示使用了鼠标的左中右3个按键,已经转珠的上下两个按键

event.key 表示使用了键盘的哪个按键
           

对于PickEvent,具有如下属性:

event.artist 表示被mouse选中的对象

event.mouseevent.xdata 用这种方式来继承MouseEvent的属性
           

使用Event编码遇到的小问题记录:

1,event.button(),由于是函数,所以有(),并且有返回值,分别是1,2,3分别表示左键,滑轮键,右键。

2,fig.canvas.mpl_connect(),表示在fig这个figure对象下的画布canvas,要实现一个获取connection的ID的功能。调用的函数是mpl_connect().所以经常有使用ax.figure来表示在ax这个axes下的figure;再使用ax.figure.canvas表示这个figure下的画布canvas。

3,  在交互模式下:plt.show()是不需要的;

     在block模式下,plt.show()是需要的

参考文献:

1. https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py

2. https://matplotlib.org/2.0.0/users/artists.html#object-containers

3. https://matplotlib.org/users/event_handling.html