gpt4 book ai didi

python - 使用 PyQt5 和 Matplotlib 制作动态图

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:21 26 4
gpt4 key购买 nike

我想读取数据并绘制动态图,所以我用PyQt5学习matplotlib。我找到了示例,但它适用于 PyQt4 there 。我将其修改为PyQt5,但出现了一些问题,当我单击开始按钮时,它显示错误

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 1305, in _on_timer ret = func(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", line 1049, in _step still_going = Animation._step(self, *args) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", line 855, in _step self._draw_next_frame(framedata, self._blit) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", line 873, in _draw_next_frame self._pre_draw(framedata, blit) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", line 886, in _pre_draw self._blit_clear(self._drawn_artists, self._blit_cache) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", line 926, in _blit_clear a.figure.canvas.restore_region(bg_cache[a]) KeyError: matplotlib.axes._subplots.AxesSubplot object at 0x1067718d0

这是我的代码,有人可以帮助我吗?

import sys, os, random
from PyQt5 import QtCore
from PyQt5.QtWidgets import *

import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.animation as animation

class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
self.axes.hold(False)

self.compute_initial_figure()

#
FigureCanvas.__init__(self, fig)
self.setParent(parent)

def compute_initial_figure(self):
pass

class AnimationWidget(QWidget):
def __init__(self):
QMainWindow.__init__(self)

vbox = QVBoxLayout()
self.canvas = MyMplCanvas(self, width=5, height=4, dpi=100)
vbox.addWidget(self.canvas)

hbox = QHBoxLayout()
self.start_button = QPushButton("start", self)
self.stop_button = QPushButton("stop", self)
self.start_button.clicked.connect(self.on_start)
self.stop_button.clicked.connect(self.on_stop)
hbox.addWidget(self.start_button)
hbox.addWidget(self.stop_button)
vbox.addLayout(hbox)
self.setLayout(vbox)

self.x = np.linspace(0, 5*np.pi, 400)
self.p = 0.0
self.y = np.sin(self.x + self.p)
self.line, = self.canvas.axes.plot(self.x, self.y, animated=True, lw=2)




def update_line(self, i):
self.p += 0.1
y = np.sin(self.x + self.p)
self.line.set_ydata(y)
return [self.line]

def on_start(self):
self.ani = animation.FuncAnimation(self.canvas.figure, self.update_line,
blit=True, interval=25)

def on_stop(self):
self.ani._stop()



if __name__ == "__main__":
qApp = QApplication(sys.argv)
aw = AnimationWidget()
aw.show()
sys.exit(qApp.exec_())

最佳答案

我尝试了您的代码,似乎唯一真正的问题发生在您单击开始两次而不停止动画时。

解决此问题的一种方法(无需深入了解动画情节的细节)是防止开始和停止按钮在不应该执行的操作时执行任何操作。

实现这一点的方法是在AnimationWidget类中添加一个属性来记录动画是否正在播放。因此,请将其放在 AnimationWidget__init__ 方法中的某个位置:

self.playing = False

然后,将 on_starton_stop 方法更改为

def on_start(self):
if self.playing:
pass
else:
self.playing = True
self.ani = animation.FuncAnimation(
self.canvas.figure,
self.update_line,
blit=True, interval=25
)

def on_stop(self):
if self.playing:
self.playing = False
self.ani._stop()
else:
pass

这使得动画不会启动两次并出现错误和图形故障。

关于python - 使用 PyQt5 和 Matplotlib 制作动态图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40372103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com