使用 NavigationToolbar 进行缩放在使用 pyqt 的 matplotlib 中不起作用

Posted

技术标签:

【中文标题】使用 NavigationToolbar 进行缩放在使用 pyqt 的 matplotlib 中不起作用【英文标题】:zooming with NavigationToolbar does not work in matplotlib with pyqt 【发布时间】:2016-08-24 18:53:50 【问题描述】:

我尝试使用 python 2.7、matplotlib、qt5(5.7 版)和 pyqt5 绘制数据。我举了一个例子,并根据我的需要进行了调整。我看到 NavigationToolbar 已添加到绘图窗口,但缩放不起作用。我不确定 NavigationToolbar 对象的第二个参数应该是什么?

import sys
import matplotlib
matplotlib.use("Qt5Agg")
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication, QSizePolicy, QVBoxLayout, QPushButton
from PyQt5.QtCore import *
from PyQt5.QtGui import QCursor

class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.fig = fig
        self.axes = fig.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)

        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass


class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""
    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)


class ApplicationWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.main_widget = QWidget(self)
        self.l = QVBoxLayout(self)
        but = QPushButton("make_new", self)
        but.clicked.connect(self.again)
        self.l.addWidget(but)

    def again(self):
        sc = MyStaticMplCanvas(self, width=5, height=4, dpi=100)
        self.sc = sc

        win = MyMplCanvas()
        win.fig = self.sc.fig
        FigureCanvas.__init__(win, win.fig)
        self.win = win

        self.mpl_toolbar = NavigationToolbar(self.sc, self.win)
        win.show()

qApp = QApplication(sys.argv)
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())

【问题讨论】:

【参考方案1】:

如果有人也感兴趣,我是这样解决的:

import sys
import matplotlib
matplotlib.use("Qt5Agg")
import numpy as np
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication, QSizePolicy, QVBoxLayout, QPushButton
from PyQt5.QtCore import *
from PyQt5.QtGui import QCursor

class MyMplCanvas(FigureCanvas):
    #Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.fig = fig
        self.axes = fig.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)

        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass


class MyStaticMplCanvas(MyMplCanvas):
    #Simple canvas with a sine plot.
    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)

class PlotDialog(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.plot_layout = QVBoxLayout(self)
        self.plot_canvas = MyStaticMplCanvas(self, width=5, height=4, dpi=100)

        self.navi_toolbar = NavigationToolbar(self.plot_canvas, self)
        self.plot_layout.addWidget(self.plot_canvas)  # the matplotlib canvas
        self.plot_layout.addWidget(self.navi_toolbar)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    dialog = PlotDialog()
    dialog.show()
    sys.exit(app.exec_())

【讨论】:

以上是关于使用 NavigationToolbar 进行缩放在使用 pyqt 的 matplotlib 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在 NavigationToolbar2QT 中释放平移功能

PyQt5 结合 matplotlib 时,如何显示其 NavigationToolbar

Tkinter 中的 Matplotlib 绘图 - 每次更新都会添加新的 NavigationToolbar?

Matplotlib NavigationToolbar小部件未显示在Tkinter GUI中

Linux 升级后如何修复“TypeError:navigationToolbar2QT.message[str] 和 _show_message() 之间的连接()失败”?

我们可以在 Android 中使用缩放手势检测器进行缩放吗?