【发布时间】:2018-09-07 09:02:26
【问题描述】:
这是一个说明我的问题的玩具代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], '-o', animated=True)
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
ax.set_xlim(np.amin(xdata), np.amax(xdata))
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
如果我设置blit=True,那么数据点将按照我想要的方式绘制。但是,x 轴标签/刻度保持不变。
如果我设置blit=False,那么 x 轴标签和刻度会按照我想要的方式更新。但是,从未绘制过任何数据点。
我怎样才能同时获得绘制的数据(正弦曲线)和要更新的 x 轴数据”?
【问题讨论】:
-
使用
animated=False和blit=False。我可能会写一个完整的答案,并解释为什么稍后会这样做。 -
@ImportanceOfBeingErnest WOT?!好的,我非常感谢您在这里进行深入的解释。我很困惑。顺便说一句,爱你的名字!好戏……☺
标签: python animation matplotlib