pyqt5でのmatplotlibによるグラフ表示がうまくいかない
python3.5を使っています。pyqt5でmatplotlibを使ってみたところ、matplotlibで使えていた一部の機能が使えなくなりました。具体的にはx、y軸のラベル、グラフのタイトルが表示されないほか、x、y軸の表示範囲設定ができなりました。コードをどのように直せばよいのでしょうか。
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
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
import numpy as np
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)
self.grid_cb = QCheckBox("Show &Grid")
self.grid_cb.setChecked(False)
self.grid_cb.stateChanged.connect(self.plot) #int
# set the layout
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.grid_cb)
btnlayout = QtWidgets.QHBoxLayout()
btnlayout.addWidget(self.button1)
btnlayout.addWidget(self.button2)
btnlayout.addWidget(self.button3)
btnlayout.addWidget(self.button4)
#btnlayout.addWidget(self.grid_cb)
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):
x = np.arange(0, 4*np.pi, 0.1)
y=np.sin(x)
self.axes.set_xlim(0,5)
self.axes.set_ylim(-1.2,1.2)
self.axes.set_xlabel("angle")
self.axes.set_ylabel("intensity")
self.axes.set_title("E")
self.axes.plot(x,y,c="r")
self.axes.grid(self.grid_cb.isChecked())
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_())