matplotlib NavigationToolbar2 回调(例如 release_zoom())语法示例? [英] matplotlib NavigationToolbar2 callbacks (e.g. release_zoom()) syntax example?

查看:77
本文介绍了matplotlib NavigationToolbar2 回调(例如 release_zoom())语法示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到各种NavigationToolbar2回调的工作代码示例.我已阅读文档a>,但仍在努力学习,并且没有任何幸运的定位代码示例来说明如何正确连接到我感兴趣的事件.

为了具体起见,让我们只关注如何将代码附加到 release_zoom()?"

以上链接提供了此文档:

release_pan(event)-在平移/缩放模式下释放鼠标按钮的回调.

以下(错误的)工作示例中感兴趣的行是:

self.nt.release_zoom('button_release_event')self.canvas.mpl_connect('button_release_event', self.on_rel_zoom1)self.canvas.mpl_connect('release_zoom',self.on_rel_zoom2)

我只能连接到 button_release_event.如何正确连接到release_zoom()?

从PyQt5导入QtWidgets

 从matplotlib.backends.backend_qt5agg导入FigureCanvasQTAgg作为FigureCanvas从matplotlib.backends.backend_qt5agg导入NavigationToolbar2QT作为NavigationToolbarfrom matplotlib.figure 导入图将numpy导入为npPlotWin(QtWidgets.QMainWindow)类:def __init __(self,parent = None):超级(PlotWin,自我).__ init __(父母)QtWidgets.QMainWindow.__init__(self, parent)self.win = QtWidgets.QWidget(self)self.setCentralWidget(self.win)布局= QtWidgets.QVBoxLayout(self.win)self.canvas = FigureCanvas(Figure())layout.addWidget(self.canvas)self.nt = NavigationToolbar(self.canvas,self)layout.addWidget(self.nt)self.ax1 = self.canvas.figure.add_subplot(111)self.ax1.plot(np.linspace(1, 100, 100), np.random.rand(100, 1))self.nt.release_zoom('button_release_event')self.canvas.mpl_connect('button_release_event', self.on_rel_zoom1)self.canvas.mpl_connect('release_zoom',self.on_rel_zoom2)def on_rel_zoom1(事件):打印('一个')def on_rel_zoom2(自身,事件):打印('Two')如果 __name__ == '__main__':导入系统如果不是QtWidgets.QApplication.instance():app = QtWidgets.QApplication(sys.argv)别的:app = QtWidgets.QApplication.instance()窗口 = PlotWin()window.show()app.exec_()

解决方案

release_pan release_zoom 是回调.这些是当用户在平移或缩放模式下释放鼠标的条件满足时调用的函数.您无法连接它们,但您可以将它们连接到,当然您可以在需要时调用它们.

可能要连接的事件列在 这样的东西,这个也不能连接.

但是..如果需要,您可以自己创建此"release_zoom_event".这基本上在 matplotlib 挂钩到 home/back/forward 中有描述按钮事件中的主页"按钮.将该示例修改为 "release_zoom_event" 将如下所示:

 将matplotlib.pyplot导入为plt从 matplotlib.backend_bases 导入 NavigationToolbar2release_zoom = NavigationToolbar2.release_zoomdef new_release_zoom(self, *args, **kwargs):s = 'release_zoom_event'self.canvas.callbacks.process(s, args[0])release_zoom(self, *args, **kwargs)NavigationToolbar2.release_zoom = new_release_zoomdef handle_release_zoom(evt):打印('release_zoom_event')打印(evt.xdata,evt.ydata)无花果= plt.figure()fig.canvas.mpl_connect('release_zoom_event', handle_release_zoom)plt.plot([1,3,1])plt.show()

I am not able to find a working code example for the various NavigationToolbar2 callbacks. I have read through the docs, but am still working my way up the learning curve and am not having any luck locating code examples showing how to properly connect to the events I'm interested in.

For specificity, let's focus only on "How do I attach code to release_zoom()?"

The above link provides this documentation:

release_pan(event) - Callback for mouse button release in pan/zoom mode.

Lines of interest in the (incorrectly) working example below are:

self.nt.release_zoom('button_release_event')
self.canvas.mpl_connect('button_release_event', self.on_rel_zoom1)
self.canvas.mpl_connect('release_zoom', self.on_rel_zoom2)

I only manage to connect to the button_release_event. How do I correctly connect to release_zoom()?

from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import numpy as np


class PlotWin(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(PlotWin, self).__init__(parent)
        QtWidgets.QMainWindow.__init__(self, parent)

        self.win = QtWidgets.QWidget(self)
        self.setCentralWidget(self.win)
        layout = QtWidgets.QVBoxLayout(self.win)

        self.canvas = FigureCanvas(Figure())
        layout.addWidget(self.canvas)
        self.nt = NavigationToolbar(self.canvas, self)
        layout.addWidget(self.nt)
        self.ax1 = self.canvas.figure.add_subplot(111)
        self.ax1.plot(np.linspace(1, 100, 100), np.random.rand(100, 1))

        self.nt.release_zoom('button_release_event')
        self.canvas.mpl_connect('button_release_event', self.on_rel_zoom1)
        self.canvas.mpl_connect('release_zoom', self.on_rel_zoom2)

    def on_rel_zoom1(self, event):
        print('One')

    def on_rel_zoom2(self, event):
        print('Two')


if __name__ == '__main__':
    import sys

    if not QtWidgets.QApplication.instance():
        app = QtWidgets.QApplication(sys.argv)
    else:
        app = QtWidgets.QApplication.instance()
    window = PlotWin()
    window.show()
    app.exec_()

解决方案

release_pan or release_zoom are callbacks. Those are the functions which are called once the conditions of the user releasing the mouse in pan or zoom mode are satisfied. You cannot connect them, but you may connect to them and of course you may call them if needed.

The possible events to connect are listed in the documentation. They are

events = ['resize_event', 
          'draw_event', 
          'key_press_event', 
          'key_release_event', 
          'button_press_event', 
          'button_release_event', 
          'scroll_event', 
          'motion_notify_event', 
          'pick_event', 
          'idle_event', 
          'figure_enter_event', 
          'figure_leave_event', 
          'axes_enter_event', 
          'axes_leave_event', 
          'close_event']

Since there is no such thing as a "release_zoom_event", this can also not be connected.

But.. you may create this "release_zoom_event" yourself, if needed. This is essentially described in matplotlib hooking in to home/back/forward button events for the "home" button. Adapting that example to the "release_zoom_event" would look like this:

import matplotlib.pyplot as plt
from matplotlib.backend_bases import NavigationToolbar2

release_zoom = NavigationToolbar2.release_zoom

def new_release_zoom(self, *args, **kwargs):
    s = 'release_zoom_event'
    self.canvas.callbacks.process(s, args[0])
    release_zoom(self, *args, **kwargs)

NavigationToolbar2.release_zoom = new_release_zoom

def handle_release_zoom(evt):
    print('release_zoom_event')
    print(evt.xdata,evt.ydata)

fig = plt.figure()
fig.canvas.mpl_connect('release_zoom_event', handle_release_zoom)
plt.plot([1,3,1])
plt.show()

这篇关于matplotlib NavigationToolbar2 回调(例如 release_zoom())语法示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆