很简单的实现matplotlib绑定到PyQt5
【知识点】
1 import matplotlib 2 matplotlib.use("Qt5Agg") 3 4 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 5 from matplotlib.figure import Figure
【效果图】
【源代码】
1 import numpy as np 2 import matplotlib 3 matplotlib.use("Qt5Agg") 4 5 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 6 from matplotlib.figure import Figure 7 8 from PyQt5 import QtWidgets 9 10 11 class Widget(FigureCanvas): 12 def __init__(self, parent=None, width=5, height=4, dpi=100): 13 fig = Figure(figsize=(width, height), dpi=100) 14 FigureCanvas.__init__(self, fig) 15 self.setParent(parent) 16 17 # 散点图 18 self.axes = fig.add_subplot(211) 19 self.axes.scatter(np.random.rand(20), np.random.rand(20)) 20 21 # 折线图 22 self.axes2 = fig.add_subplot(212) 23 x = np.arange(0, 5, 0.1) 24 self.axes2.plot(x, np.sin(x), x, np.cos(x)) 25 26 self.setWindowTitle("示例:matplotlib 绑定到 PyQt5") 27 28 if __name__ == "__main__": 29 import sys 30 app = QtWidgets.QApplication(sys.argv) 31 w = Widget() 32 w.show() 33 sys.exit(app.exec_())