知识共享:Matplotlib按钮和鼠标处理程序

知识共享:Matplotlib按钮和鼠标处理程序,matplotlib,Matplotlib,在从事与工作相关的项目时,我了解到这个很棒的库中许多与GUI相关的主题都没有很好的文档记录,不允许使用交互式程序 经过数小时的信息收集和调试,我决定与大家分享以下发现: 如何在绘图区域下方放置按钮 如何声明按钮和鼠标单击的处理程序 如何知道单击是否发生在上部打印、下部打印或其中一个按钮上 如何对多个按钮使用相同的按钮单击处理程序 如何定位绘图窗口 我相信这将帮助其他程序员 以下代码基于此处提供的示例: 1。如何在绘图区域下方放置按钮。 如图所示。其思想是在图形坐标中创建一个轴,使其位于主轴下方。

在从事与工作相关的项目时,我了解到这个很棒的库中许多与GUI相关的主题都没有很好的文档记录,不允许使用交互式程序

经过数小时的信息收集和调试,我决定与大家分享以下发现:

  • 如何在绘图区域下方放置按钮
  • 如何声明按钮和鼠标单击的处理程序
  • 如何知道单击是否发生在上部打印、下部打印或其中一个按钮上
  • 如何对多个按钮使用相同的按钮单击处理程序
  • 如何定位绘图窗口

  • 我相信这将帮助其他程序员

    以下代码基于此处提供的示例:


    1。如何在绘图区域下方放置按钮。

    如图所示。其思想是在图形坐标中创建一个轴,使其位于主轴下方。主轴的y坐标由
    图subplopars.bottom
    给出。所以在

    ax_button = plt.axes([x0, y0, width, height])
    
    只要
    y0+高度
    按钮将位于轴下方

    2。如何声明按钮和鼠标单击的处理程序。

    按钮可以通过
    注册回调。单击时
    。仅当单击按钮时,才会调用其中的任何函数

    一般的
    “按钮按下事件”
    需要确保只在选择的轴内单击时执行某些操作

    def on_mouse_click(event):
        if event.inaxes == ax:
            # do something
    
    cid = fig.canvas.mpl_connect('button_press_event', on_mouse_click)
    
    3。如何知道单击是发生在上部打印、下部打印还是其中一个按钮上。

    与2非常相似:如果有4个轴,
    ax1
    ax2
    ax\u按钮1
    ax\u按钮2
    ,只需检查
    事件的值。inaxes
    即可知道事件发生在哪个轴上

    def on_mouse_click(event):
        if event.inaxes == ax1:
            # do something
        elif event.inaxes == ax2:
            # do something else
    
    4。如何对多个按钮使用相同的按钮单击处理程序。

    与上面类似,您可以检查事件发生的轴

    def on_button_click(event):
        if event.inaxes == btn1.ax:
            # do something
        elif event.inaxes == btn2.ax:
            # do something else
    
    btn1.on_clicked(on_button_click)
    btn2.on_clicked(on_button_click)
    
    或者,您可以将标识符传递给函数

    def on_button_click(event, id):
        if id == "button1":
            # do something
        elif id == "button2":
            # do something else
    
    btn1.on_clicked(lambda e: on_button_click(e, "button1")
    btn2.on_clicked(lambda e: on_button_click(e, "button2")
    
    5。如何定位绘图窗口。

    这是不相关的,答案将取决于使用哪个后端


    完整示例:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button
    
    
    def on_mouse_click(event):
        if event.inaxes == ax1:
            client = 'upper'
        if event.inaxes == ax2:  
            client = 'lower'
        else:
            client = 'none'
        print(client)
    
    
    def on_button_click(btn):
        print(btn)
        if btn == 'Upper':
            ax1.clear()
        elif btn == 'Lower':
            ax2.clear()
        fig.canvas.draw_idle()
    
    # First create some toy data:
    x = np.linspace(0, 2*np.pi, 400)
    y1 = np.sin(x**2)
    y2 = np.cos(x**2)
    
    # Creates two subplots and unpacks the output array immediately
    fig, (ax1, ax2) = plt.subplots(num=1, ncols=1, nrows=2, figsize=(5,8))
    
    ax1.plot(x, y1)
    ax2.plot(x, y2)
    
    ax1.set_title('Upper')
    ax2.set_title('Lower')
    
    cid = fig.canvas.mpl_connect('button_press_event', on_mouse_click)
    
    ax_button1 = fig.add_axes([0.69, 0.01, 0.1, 0.055])
    ax_button2 = fig.add_axes([0.80, 0.01, 0.1, 0.055])
    
    btn1 = Button(ax_button1, "Upper")
    btn1.on_clicked(lambda e: on_button_click("Upper"))
    btn2 = Button(ax_button2, "Lower")
    btn2.on_clicked(lambda e: on_button_click("Lower"))
    
    plt.show()
    

    把你的答案标记为已接受,而这是我自己的知识分享,这有点可笑。我真的很喜欢你的lambda解决方案,因为它提供了发件人id。谢谢!在您的代码中,请将event.inaxes==ax2:
    修复为
    elif
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import Button
    
    
    def on_mouse_click(event):
        if event.inaxes == ax1:
            client = 'upper'
        if event.inaxes == ax2:  
            client = 'lower'
        else:
            client = 'none'
        print(client)
    
    
    def on_button_click(btn):
        print(btn)
        if btn == 'Upper':
            ax1.clear()
        elif btn == 'Lower':
            ax2.clear()
        fig.canvas.draw_idle()
    
    # First create some toy data:
    x = np.linspace(0, 2*np.pi, 400)
    y1 = np.sin(x**2)
    y2 = np.cos(x**2)
    
    # Creates two subplots and unpacks the output array immediately
    fig, (ax1, ax2) = plt.subplots(num=1, ncols=1, nrows=2, figsize=(5,8))
    
    ax1.plot(x, y1)
    ax2.plot(x, y2)
    
    ax1.set_title('Upper')
    ax2.set_title('Lower')
    
    cid = fig.canvas.mpl_connect('button_press_event', on_mouse_click)
    
    ax_button1 = fig.add_axes([0.69, 0.01, 0.1, 0.055])
    ax_button2 = fig.add_axes([0.80, 0.01, 0.1, 0.055])
    
    btn1 = Button(ax_button1, "Upper")
    btn1.on_clicked(lambda e: on_button_click("Upper"))
    btn2 = Button(ax_button2, "Lower")
    btn2.on_clicked(lambda e: on_button_click("Lower"))
    
    plt.show()