Matplotlib"pick_event"无法与FigureCanvasTkAgg在嵌入式图形中工作 [英] Matplotlib "pick_event" not working in embedded graph with FigureCanvasTkAgg
问题描述
我正在尝试处理一些事件,以与Tkinter框架中的嵌入式子图进行用户交互.像这样的示例
I'm trying to handle some events to perform user interactions with embedded subplots into a Tkinter frame. Like in this example
与"key_press_event"和"button_press_event"一起正常工作,但不适用于"pick_event".
Works fine with "key_press_event" and "button_press_event", but does not work with "pick_event".
我从链接中修改了该示例,只是在mpl_connect
调用之后添加了以下代码:
I modified that example from the link, just adding the following piece of code after the mpl_connect
calling:
def on_button_press(event):
print('you pressed mouse button')
canvas.mpl_connect('button_press_event', on_button_press)
def on_pick(event):
print('you picked:',event.artist)
canvas.mpl_connect('pick_event', on_pick)
为什么"pick_event"在嵌入的图形中不起作用?以及如何使其工作?
Why "pick_event" doesn't work into embedded graphs? And how do get it to work?
我的配置详细:
- Windows 10
- Python 3.5(conda版本)
- 通过pip安装的Matplotlib 1.5.3
提前谢谢!
推荐答案
好,我解决了...
大多数事件我们只需要使用mpl_connect
方法即可发生.我的错误是,我没有注意到我们需要明确地说我们的情节是可拾取的",将参数picker=True
仅在完全单击艺术家的情况下才会触发事件,而picker=x
其中x
是整数,它是触发器的像素容限.因此,除了我在问题中插入的更改之外,我们还应该替换
Most events we just need to use mpl_connect
method to the magic happen. My mistake is that I didn't notice that we need to say explictly that our plot is "pickable" putting a argument picker=True
to only triggers the event if clicked exacly into the artist, and picker=x
where x
is an integer that is the pixel tolerance for the trigger. So beyond the changes I inserted for pick in the question, we should replace
a.plot(t, s)
用于a.plot(t, s,picker=True)
或a.plot(t, s,picker=10)
,例如
这篇关于Matplotlib"pick_event"无法与FigureCanvasTkAgg在嵌入式图形中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!