如何在pyqt中embeddedmatplotlib – 对于傻瓜
我目前正在试图embedded一个图,我想绘制在我devise的pyqt4用户界面。 由于我对编程几乎是全新的 – 我不明白人们是如何embedded我发现的例子 – 这一个(底部)和那个例子。
如果任何人可以发布一步一步的解释,或者至less是一个非常小的,非常简单的代码,只需要在一个pyqt4 GUI中创build一个graphics和一个button,那就太棒了。
实际上并不复杂。 相关的Qt小部件在matplotlib.backends.backend_qt4agg
。 FigureCanvasQTAgg
和NavigationToolbar2QT
通常是您所需要的。 这些是常规的Qt小部件。 你把它们当作其他的小部件。 下面是一个非常简单的例子, Figure
, Navigation
和一个button,绘制一些随机数据。 我已经添加了评论来解释事情。
import sys from PyQt4 import QtGui from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar from matplotlib.figure import Figure import random class Window(QtGui.QDialog): def __init__(self, parent=None): super(Window, self).__init__(parent) # a figure instance to plot on self.figure = Figure() # this is the Canvas Widget that displays the `figure` # it takes the `figure` instance as a parameter to __init__ self.canvas = FigureCanvas(self.figure) # this is the Navigation widget # it takes the Canvas widget and a parent self.toolbar = NavigationToolbar(self.canvas, self) # Just some button connected to `plot` method self.button = QtGui.QPushButton('Plot') self.button.clicked.connect(self.plot) # set the layout layout = QtGui.QVBoxLayout() layout.addWidget(self.toolbar) layout.addWidget(self.canvas) layout.addWidget(self.button) self.setLayout(layout) def plot(self): ''' plot some random stuff ''' # random data data = [random.random() for i in range(10)] # create an axis ax = self.figure.add_subplot(111) # discards the old graph ax.clear() # plot data ax.plot(data, '*-') # refresh canvas self.canvas.draw() if __name__ == '__main__': app = QtGui.QApplication(sys.argv) main = Window() main.show() sys.exit(app.exec_())
编辑 :
已更新以反映评论和API更改。
-
NavigationToolbar2QTAgg
使用NavigationToolbar2QT
更改 - 直接导入
Figure
而不是pyplot
- 用
ax.hold(False)
replace弃用的ax.hold(False)
ax.clear()
下面是在PyQt5和Matplotlib 2.0下使用的代码的改编。 有一些小的变化:PyQt子模块的结构,从matplotlib的其他子模块,不推荐使用的方法已被取代…
import sys from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar import matplotlib.pyplot as plt import random class Window(QDialog): def __init__(self, parent=None): super(Window, self).__init__(parent) # a figure instance to plot on self.figure = plt.figure() # this is the Canvas Widget that displays the `figure` # it takes the `figure` instance as a parameter to __init__ self.canvas = FigureCanvas(self.figure) # this is the Navigation widget # it takes the Canvas widget and a parent self.toolbar = NavigationToolbar(self.canvas, self) # Just some button connected to `plot` method self.button = QPushButton('Plot') self.button.clicked.connect(self.plot) # set the layout layout = QVBoxLayout() layout.addWidget(self.toolbar) layout.addWidget(self.canvas) layout.addWidget(self.button) self.setLayout(layout) def plot(self): ''' plot some random stuff ''' # random data data = [random.random() for i in range(10)] # instead of ax.hold(False) self.figure.clear() # create an axis ax = self.figure.add_subplot(111) # discards the old graph # ax.hold(False) # deprecated, see above # plot data ax.plot(data, '*-') # refresh canvas self.canvas.draw() if __name__ == '__main__': app = QApplication(sys.argv) main = Window() main.show() sys.exit(app.exec_())