我想从一个随时间增长的数据数组绘制一个图。我发现 ion() 来重绘我的情节,添加新点。现在,添加一个新点应该删除一个旧点,为了实现这一点,我必须添加 clf()。这再次意味着我每次绘图时都必须重置我的轴编辑,但它忽略了依赖于轴句柄的每一个修改。我想知道这是否是因为我调用的函数而导致的范围问题?我是 python 新手,如果有比选择的方法更直接的方法,我也会感谢反馈。
我试图通过不同的功能传递轴手柄,希望这会改变事情,但没有成功。
import matplotlib.pyplot as plt
import matplotlib.ticker as tck
from time import time
x, y = [], []
counter = 0
plt.ion()
fig, ax1 = plt.subplots() # ax1 is not used
def axis(ax):
ax.set_label("Time [s]")
ax.yaxis.set_major_locator(tck.MultipleLocator(base=0.5))
def plot():
plt.clf()
ax = plt.gca()
axis(ax)
if len(y) < 3:
plt.plot(x, y, c='r')
else:
plt.plot(x[-3:], y[-3:], c='r')
plt.draw()
return ax
for i in range(0,10):
x.append(time())
y.append(counter)
print(i, '\n')
ax = plot()
counter +=1
plt.pause(1)