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