我想根据随时间增长的数据数组绘制图表。我发现 ion() 可以重新绘制我的图,添加新点。现在,添加一个新点应该会删除旧点,为了实现这一点,我必须添加 clf()。这再次意味着我每次绘图时都必须重置 Axis 编辑,但它会忽略依赖于 Axis handle 的每个修改。我想知道这是否是由于我调用的函数而导致的范围问题?我是 python 新手,如果有比所选方法更直接的方法,我也希望得到反馈。
我尝试通过不同的函数传递 Axis 句柄,希望这会改变事情,但没有成功。
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() 解决了该问题。
关于python - matplotlib 中交互模式 ion() 中的 Axis 编辑被忽略 - 范围问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637777/