40490


python3.5を使っています。pyqt5でmatplotlibを使いたいです。しかし、コードを書いてみたところ、ボタンは表示されるのですが、グラフが表示されません。  
（レイアウトでなくて座標で指示したところにグラフを置きたかったので表示個所は意図的にコメントアウ‌​トしています）

[code]

    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)
    
            self.button2 = QtWidgets.QPushButton('Zoom',self)
            self.button2.clicked.connect(self.zoom)
            self.button2.move(100,400)
    
            self.button3 = QtWidgets.QPushButton('Pan',self)
            self.button3.clicked.connect(self.pan)
            self.button3.move(200,400)
    
            self.button4 = QtWidgets.QPushButton('Home',self)
            self.button4.clicked.connect(self.home)
            self.button4.move(300,400)
    
            """
            # set the layout
            layout = QtWidgets.QVBoxLayout()
            layout.addWidget(self.toolbar)
            layout.addWidget(self.canvas)
    
            btnlayout = QtWidgets.QHBoxLayout()
            btnlayout.addWidget(self.button1)
            btnlayout.addWidget(self.button2)
            btnlayout.addWidget(self.button3)
            btnlayout.addWidget(self.button4)
            qw = QtWidgets.QWidget(self)
            qw.setLayout(btnlayout)
            layout.addWidget(qw)
    
            self.setLayout(layout)
            """
    
        def home(self):
            self.toolbar.home()
        def zoom(self):
            self.toolbar.zoom()
        def pan(self):
            self.toolbar.pan()
    
        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_())
    
[/code]


CC BY-SA 3.0
2017-12-20T09:10:55.523
40422
2021-03-09T04:54:07.563
2021-03-09T04:54:07.563
3060
26529
question
python python3 pyqt5
pyqt5でmatplotlibを使いたい


`self.canvas = FigureCanvas(self.figure)` の下に `self.canvas.setParent(self)` と
`self.canvas.move(100,20)` を加えることで無事動きました。

**修正後のプログラム:**

[code]

    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.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()
    
        sys.exit(app.exec_())
    
[/code]


CC BY-SA 4.0
2017-12-23T06:30:57.263
40490
2021-03-09T04:53:13.050
2021-03-09T04:53:13.050
3060
26529
40422
answer