Matplotlib按钮

3

Matplotlib小部件按钮事件和fig.canvas.mpl_connect('button_press_event')都会在鼠标点击按钮时触发。

我的问题是:

1)如何使fig.canvas.mpl_connect('button_press_event')事件具有更高的优先级? 并且 2)如何在fig.canvas.mpl_connect('button_press_event')事件中判断是否正在单击按钮。

import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons


fig = plt.figure()
# plotting
X=[1,2,3]
Y=[10,20,30]
ax  = fig.add_subplot(1, 1, 1)
ax.plot(X,Y,'bo-')
ax.grid()
ax.legend()
X1=[]
Y1=[]

def on_press(event):
    print "canvas clicked"
    print "how can I tell whether the button is clicked?"
    print event
def on_button_clicked(event):
    print "button clicked"
    print event
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(on_button_clicked)
fig.canvas.mpl_connect('button_press_event', on_press)
plt.show()
2个回答

2
关于第一个问题,为什么需要它?在这种情况下你能忽略事件吗?
关于第二个问题,您可以使用提取按钮的坐标,并将其与鼠标事件的坐标进行比较,如下面的示例所示:
import matplotlib.pylab as plt
from matplotlib.widgets import Button


fig,ax = plt.subplots()
ax.plot([1,2,3],[10,20,30],'bo-')
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')

(xm,ym),(xM,yM)=bnext.label.clipbox.get_points()

def on_press(event):

    if xm<event.x<xM and ym<event.y<yM:
        print "Button clicked, do nothing. This triggered event is useless."
    else:
        print "canvas clicked and Button not clicked. Do something with the canvas."
    print event
def on_button_clicked(event):
    print "button clicked, do something triggered by the button."
    print event

bnext.on_clicked(on_button_clicked)
fig.canvas.mpl_connect('button_press_event', on_press)
plt.show()

0

你也可以使用事件发生的轴,就像下面的代码一样。 请注意,将文本插入绘图中而不是使用打印语句,只是因为我在Jupyter中开发此代码,并且交互式绘图不显示打印语句。

import numpy as np
import matplotlib.pylab as plt
from matplotlib.widgets import Button, CheckButtons
import time

plt.close('all')
fig,ax = plt.subplots()
ax.plot([1,2,3],[10,20,30],'bo-')
axcnt=0
plt.subplots_adjust(bottom=0.25)

axnext = plt.axes([0.81, 0.05, 0.1, 0.075]) #l,b,w,h
bnext = Button(axnext, 'Next')
nxtcnt=0

axchk = plt.axes([0.0, 0.05, 0.2, 0.125]) #l,b,w,h 
bchk = CheckButtons(axchk, ('Check',))
chkcnt=0

def on_press(event):
    global cancnt; cancnt += 1
    txt = plt.figtext(0.2,0.3,f"canvas clicked {cancnt}")
    fig.canvas.draw(); time.sleep(2); txt.remove(); fig.canvas.draw()
    
    
    if event.inaxes == ax:
        global axcnt; axcnt += 1
        txt = plt.figtext(0.4,0.0,f"ax clicked {axcnt}")
        fig.canvas.draw(); time.sleep(2); txt.remove(); fig.canvas.draw()
        
def on_next_button_clicked(event):
    global nxtcnt; nxtcnt += 1
    txt = plt.figtext(0.7,0.0, f"next button clicked {nxtcnt}")
    fig.canvas.draw(); time.sleep(2); txt.remove(); fig.canvas.draw()
    
def on_chk_button_clicked(event):
    global chkcnt; chkcnt += 1
    txt = plt.figtext(0.0,0.0, f"check button clicked {chkcnt}")
    fig.canvas.draw(); time.sleep(2); txt.remove(); fig.canvas.draw()

bnext.on_clicked(on_next_button_clicked)
bchk.on_clicked(on_chk_button_clicked)
            
fig.canvas.mpl_connect('button_press_event', on_press)
cancnt=0
txt = plt.figtext(0.2,0.9,f"test {cancnt}")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接