简体   繁体   English

我想在pyqt5中使用matplotlib,但它不显示图形

[英]I want to use matplotlib in pyqt5, but it doesn't show graph

I use python3.5. 我使用python3.5。 I want to use matplotlib in pyqt5, and I wrote below code. 我想在pyqt5中使用matplotlib,我写了以下代码。 But although it shows buttons, it doesn't show graph. 但是,尽管它显示按钮,但不显示图形。 I'd like to show graph in the coordinate I hope. 我想在希望的坐标中显示图表。 So, I don't want to use "layout" command. 因此,我不想使用“布局”命令。

import sys
from PyQt5 import QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

import random

class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setWindowTitle("グラフ")
        self.setGeometry(300,300,500,500)

        self.figure = plt.figure()
        self.axes = self.figure.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)
        self.canvas = FigureCanvas(self.figure)
        self.canvas.move(0,0)


        self.toolbar = NavigationToolbar(self.canvas, self)
        self.toolbar.hide()

        # Just some button 
        self.button1 = QtWidgets.QPushButton('Plot',self)
        self.button1.clicked.connect(self.plot)
        self.button1.move(0,400)

    def plot(self):
        ''' plot some random stuff '''
        data = [random.random() for i in range(25)]
        self.axes.plot(data, '*-')
        self.canvas.draw()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    main = Window()
    main.setWindowTitle('Simple QTpy and MatplotLib example with Zoom/Pan')
    main.show()

    sys.exit(app.exec_())

A widget that wants to be drawn on must be a child, or have the main widget as an ancestor, and in your case self.canvas has no parent, so the solution is to pass one with the setParent() method: 要绘制的窗口小部件必须是子窗口,或将主窗口小部件作为祖先,在您的情况下,self.canvas没有父窗口,因此解决方案是通过setParent()方法传递一个窗口小部件:

class Window(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setWindowTitle("グラフ")
        self.setGeometry(300,300,800,500)

        self.figure = plt.figure()
        self.axes = self.figure.add_subplot(111)
        # We want the axes cleared every time plot() is called
        self.axes.hold(False)
        self.canvas = FigureCanvas(self.figure)

        self.canvas.setParent(self)

        self.canvas.move(100,20)

        self.toolbar = NavigationToolbar(self.canvas, self)
        self.toolbar.hide()

        # Just some button 
        self.button1 = QtWidgets.QPushButton('Plot',self)
        self.button1.clicked.connect(self.plot)
        self.button1.move(0,400)

    def plot(self):
        ''' plot some random stuff '''
        data = [random.random() for i in range(25)]
        self.axes.plot(data, '*-')
        self.canvas.draw()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    main = Window()
    main.setWindowTitle('Simple QTpy and MatplotLib example with Zoom/Pan')
    main.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM