我想从一个及时增长的数据数组中绘制一个图。我发现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)
不需要传递斧头。用ax1.clear()替换plt.clf()解决了这个问题。