无法在Qt应用程序中重绘matplot

时间:2017-11-12 07:18:22

标签: python qt matplotlib pyqt4 pyside

我想调用matplotlibdraw()方法,该方法用于更新已更改但未以原子方式重新绘制的数字。请注意,该图已在QT应用程序内构建。以下是错误跟踪:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/cbook/__init__.py", line 389, in process
    proxy(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/cbook/__init__.py", line 227, in __call__
    return mtd(*args, **kwargs)
  File "matplotlib_qt.py", line 43, in mouseClick
    self.myMplCanvas.fig.draw()
TypeError: draw_wrapper() takes at least 2 arguments (1 given)

在独立的matplotlib应用程序中,可以轻松地调用draw方法,如下所示:

import matplotlib.pyplot as plt

fig = plt.figure()
# plot here and change plot variables later
# now re-draw it
plt.draw()

以下是完整的QT应用程序代码:

import sys
from matplotlib.backends import qt_compat
use_pyside = qt_compat.QT_API == qt_compat.QT_API_PYSIDE
if use_pyside:
    from PySide import QtGui, QtCore
else:
    from PyQt4 import QtGui, QtCore

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.image as mpimg

class MyMplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)

        img = mpimg.imread('stinkbug.png')
        lum_img = img[:, :, 0]
        self.axes.imshow(lum_img)

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

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

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")

        self.main_widget = QtGui.QWidget(self)
        l = QtGui.QVBoxLayout(self.main_widget)
        self.myMplCanvas = MyMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(self.myMplCanvas)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)
        self.myMplCanvas.mpl_connect('button_press_event', self.mouseClick)

    def mouseClick(self, event):
        self.myMplCanvas.fig.draw()


qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("hello")
aw.show()
sys.exit(qApp.exec_())

这里出了什么问题?任何解决方法,请。

1 个答案:

答案 0 :(得分:2)

请注意,如果@SpringBootApplication public class AppConfig { public static void main(String[] args) throws IOException { SpringApplication.run(AppConfig.class, args); } @Bean public Module hibernate5Module() { return new Hibernate5Module(); } } 是数字,则plt.draw()fig.draw()不同。为了绘制图形,需要提供渲染器。

但是,您可能想绘制画布而不是图形。

fig

对于问题,您已将画布作为fig.canvas.draw() # <- not recommended # better: fig.canvas.draw_idle() ,因此您将调用

self.myMplCanvas