| 事件名称 | 类 | 描述 |
|---|---|---|
| ‘button_press_event' | MouseEvent | 鼠标按键被按下 |
| ‘button_release_event' | MouseEvent | 鼠标按键被释放 |
| ‘draw_event' | DrawEvent | 画布绘图 |
| ‘key_press_event' | KeyEvent | 键盘按键被按下 |
| ‘key_release_event' | KeyEvent | 键盘按键被释放 |
| ‘motion_notify_event' | MouseEvent | 鼠标移动 |
| ‘pick_event' | PickEvent | 画布中的对象被选中 |
| ‘resize_event' | ResizeEvent | 图形画布大小改变 |
| ‘scroll_event' | MouseEvent | 鼠标滚轮被滚动 |
| ‘figure_enter_event' | LocationEvent | 鼠标进入新的图形 |
| ‘figure_leave_event' | LocationEvent | 鼠标离开图形 |
| ‘axes_enter_event' | LocationEvent | 鼠标进入新的轴域 |
| ‘axes_leave_event' | LocationEvent | 鼠标离开轴域 |
因为matplotlib中用到的事件类都继承自matplotlib.backend_bases.Event,所以所有事件都拥有以下3个共同属性。
name:事件名称。
canvas:生成事件的canvas对象。
guiEvent:触发matplotlib事件的GUI事件,默认为None。
所有事件均定义在matplotlib.backend_bases模块中,其中常用的鼠标事件MouseEvent、键盘事件KeyEvent都继承自LocationEvent事件。LocationEvent事件有5个属性。
键盘事件KeyEvent除继承自LocationEvent事件的5个属性外,还有1个key属性,表示按下的键,值范围为:None、任何字符、'shift'、win或者control。
鼠标事件MouseEvent 除继承自LocationEvent事件的5个属性外,还有以下属性
鼠标点击画线,将鼠标点击相邻两点用直线连接,起始点为0,0。
from matplotlib import pyplot as plt
class LineBuilder:
def __init__(self, line):
self.line = line
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
print('click', event)
if event.inaxes!=self.line.axes: return
self.xs.append(event.xdata)
self.ys.append(event.ydata)
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0]) # empty line
linebuilder = LineBuilder(line)
plt.show()
到此这篇关于matplotlib事件处理基础(事件绑定、事件属性)的文章就介绍到这了,更多相关matplotlib 事件处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!