我需要你的帮助来编写一个Python脚本,它将采用动态变化的数据,这里的数据来源无关紧要,并在屏幕上显示图形.
我知道如何使用matplotlib,但matplotlib的问题是,我只能在脚本结尾处显示一次图形.我需要不仅能够一次显示图形,而且每次数据发生变化时都能动态更新图形.
我发现可以使用带有matplotlib的wxPython来实现这一点,但是对我来说这样做有点复杂,因为我根本不熟悉wxPython.
所以如果有人向我展示如何使用带有matplotlib的wxPython来显示和更新简单图形的简单示例,我将非常高兴.
或者,如果是其他方式,这对我也是好的.
PS:
好的,因为没有人回答并查看了@janislaw注意到的matplotlib帮助,并编写了一些代码.这是一个虚拟的例子:
import time
import matplotlib.pyplot as plt
def data_gen():
a=data_gen.a
if a>10:
data_gen.a=1
data_gen.a=data_gen.a+1
return range (a,a+10)
def run(*args):
background = fig.canvas.copy_from_bbox(ax.bbox)
while 1:
time.sleep(0.1)
# restore the clean slate background
fig.canvas.restore_region(background)
# update the data
ydata = data_gen()
xdata=range(len(ydata))
line.set_data(xdata, ydata)
# just draw the animated artist
ax.draw_artist(line)
# just redraw the axes rectangle
fig.canvas.blit(ax.bbox)
data_gen.a=1
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], animated=True)
ax.set_ylim(0, 20)
ax.set_xlim(0, 10)
ax.grid()
manager = plt.get_current_fig_manager()
manager.window.after(100, run)
plt.show()
此实现有问题,如脚本停止,如果您尝试移动窗口.但基本上它可以使用.