用matplotlib画图,会单独弹出绘图窗口,有没有办法让图表在主窗口显示而不弹出独立窗口
窗口的搭建用的是PyQt5,想把matplotlib的绘图结果直接在PyQt5搭建的图形界面里显示
matplotlib支持交互式绘图,这是我们最常使用的方式。此外,matplotlib也可以方便地嵌入到多个GUI库中,比如PyQt和wxPython等。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets,QtGui
import sys
matplotlib.use('Qt5Agg')
class My_Main_window(QtWidgets.QDialog):
def __init__(self,parent=None):
super(My_Main_window,self).__init__(parent)
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.button_plot = QtWidgets.QPushButton("绘制")
self.button_plot.clicked.connect(self.plot_)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.canvas)
layout.addWidget(self.button_plot)
self.setLayout(layout)
def plot_(self):
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
ax = self.figure.add_axes([0.1,0.1,0.8,0.8])
ax.plot(x, y)
self.canvas.draw()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_window = My_Main_window()
main_window.show()
app.exec()