我正在尝试用matplotlib监控实时数据. 我发现我可以在Pyplot中使用交互模式动态更新绘图. 它运作良好,但有一个问题是“我根本无法操纵数字窗口”.例如,移动或重新调整图形窗口的大小
我发现我可以在Pyplot中使用交互模式动态更新绘图.
它运作良好,但有一个问题是“我根本无法操纵数字窗口”.例如,移动或重新调整图形窗口的大小.
这是我的代码.
这是互动模式的缺点?或者我使用不正确?
import matplotlib.pyplot as plt
import time
import math
# generate data
x = [0.1*_a for _a in range(1000)]
y = map(lambda x : math.sin(x), x)
# interactive mode
plt.ion() # identical plt.interactive(True)
fig, ax = plt.subplots()
# ax = plt.gca()
lines, = ax.plot([], [])
# ax.set_ylim(-1, 1)
ax.grid()
MAX_N_DATA = 100
x_data = []
y_data = []
for i in range(len(x)):
# New data received
x_data.append(x[i])
y_data.append(y[i])
# limit data length
if x_data.__len__() > MAX_N_DATA:
x_data.pop(0)
y_data.pop(0)
# Set Data
lines.set_xdata(x_data)
lines.set_ydata(y_data)
# The data limits are not updated automatically.
ax.relim()
# with tight True, graph flows smoothly.
ax.autoscale_view(tight=True, scalex=True, scaley=True)
# draw
plt.draw()
time.sleep(0.01)
谢谢.
如 this answer to another question所示,用plt.pause(0.05)替换plt.draw().这解决了我的问题.