pyqt matplotlib动态显示图

网友投稿 851 2022-08-22

pyqt matplotlib动态显示图

pyqt matplotlib动态显示图

今天别人让我动态显示作图,他只会静态的,动态的不会,这里我分享我参考的demo代码

import sysimport randomimport matplotlibmatplotlib.use('Qt5Agg')from PyQt5 import QtCore, QtWidgetsfrom matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvasfrom matplotlib.figure import Figureclass MplCanvas(FigureCanvas): def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) super(MplCanvas, self).__init__(fig)class MainWindow(QtWidgets.QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.canvas = MplCanvas(self, width=5, height=4, dpi=100) self.setCentralWidget(self.canvas) n_data = 50 self.xdata = list(range(n_data)) self.ydata = [random.randint(0, 10) for i in range(n_data)] self.update_plot() self.show() # Setup a timer to trigger the redraw by calling update_plot. self.timer = QtCore.QTimer() self.timer.setInterval(100) self.timer.timeout.connect(self.update_plot) self.timer.start() def update_plot(self): # Drop off the first y element, append a new one. self.ydata = self.ydata[1:] + [random.randint(0, 10)] self.canvas.axes.cla() # Clear the canvas. self.canvas.axes.plot(self.xdata, self.ydata, 'r') # Trigger the canvas to update and redraw. self.canvas.draw()app = QtWidgets.QApplication(sys.argv)w = MainWindow()app.exec_()

主要就是update_plot里面的东西啦,是不是很简单。

参考文献

[1].Plotting with Matplotlib. ​​https://learnpyqt.com/courses/graphics-plotting/plotting-matplotlib/​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:mac selenium 连接已经打开的chrome浏览器
下一篇:No CUDA runtime is found, using CUDA_HOME=‘/usr/local/cuda-10.0‘
相关文章

 发表评论

暂时没有评论,来抢沙发吧~