我正在玩 matplotlib 图的动态更新。
我希望能够动态更新绘图,基于拉下一些数据,比如每 0.5 秒。但是,我希望能够使用 jpg 图像,而不是使用标记。即绘制多个图像,并沿轴移动它们。
这是一个使用标记执行该想法的虚拟代码:
import matplotlib.pyplot as plt
import random
plt.ion()
class DynamicUpdate():
#Suppose we know the x range
min_x = 0
max_x = 10
def on_launch(self):
self.figure, self.ax = plt.subplots()
self.lines, = self.ax.plot([],[], 'o')
self.ax.set_xlim(self.min_x, self.max_x)
self.ax.set_ylim(0,500)
self.ax.grid()
def on_running(self, xdata, ydata):
self.lines.set_xdata(xdata)
self.lines.set_ydata(ydata)
self.figure.canvas.draw()
self.figure.canvas.flush_events()
#Example
def __call__(self):
import numpy as np
import time
self.on_launch()
xdata = np.arange(10)
ydata = np.zeros(10)
for it in range(100):
ydata=[y+random.randint(1,10) for y in ydata]
self.on_running(xdata, ydata)
time.sleep(0.5)
return xdata, ydata
d = DynamicUpdate()
d()
plt.show()
我曾尝试使用imshow()
将图像添加到轴,但它们拒绝随着数据的变化而更新和移动。
如果有人有任何好主意,我将不胜感激。