【发布时间】:2021-11-10 03:32:20
【问题描述】:
我想用我的笔记本中的每个时间步更新一个 Matplotlib 图形。我在 StackOverflow 上引用了许多 API resources 和 examples 论坛答案,但是,所有建议的代码要么 did not graph 要么在每个时间步显示一个新数字,如 this answer,
import matplotlib.pyplot as plt
import time
import random
from collections import deque
import numpy as np
# simulates input from serial port
def random_gen():
while True:
val = random.randint(1,10)
yield val
time.sleep(0.1)
a1 = deque([0]*100)
ax = plt.axes(xlim=(0, 20), ylim=(0, 10))
d = random_gen()
line, = plt.plot(a1)
plt.ion()
plt.ylim([0,10])
plt.show()
for i in range(0,20):
a1.appendleft(next(d))
datatoplot = a1.pop()
line.set_ydata(a1)
plt.draw()
print a1[0]
i += 1
time.sleep(0.1)
plt.pause(0.0001) #add this it will be OK.
import numpy as np
import matplotlib.pyplot as plt
plt.axis([0, 10, 0, 1])
for i in range(10):
y = np.random.random()
plt.scatter(i, y)
plt.pause(0.1)
如何在 Python 中通过 Matplotlib 或可能的其他方式更新每个时间步长的图形?我很欣赏你的观点。
谢谢你:)
【问题讨论】:
标签: python matplotlib plot dynamic