【问题标题】:Matplotlib ButtonMatplotlib 按钮
【发布时间】:2014-01-11 19:18:34
【问题描述】:

Matplotlib Widget Buttons 事件和 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()

【问题讨论】:

    标签: button matplotlib widget


    【解决方案1】:

    关于第一点,你为什么需要它?在这种情况下你能忽略这个事件吗?

    关于第二点,您可以使用bnext.label.clipbox.get_points()提取按钮的坐标,并将其与鼠标事件的坐标进行比较,如下例:

    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()
    

    【讨论】:

      【解决方案2】:

      您还可以使用发生事件的轴,如下面的代码所示。 请注意,文本被插入到绘图中而不是使用打印只是因为我在 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}")
      

      【讨论】:

        猜你喜欢
        • 2015-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 2017-09-04
        • 2019-07-23
        • 1970-01-01
        相关资源
        最近更新 更多