我使用python和matplotlib创建条形图竞赛动画,数据是时间序列并且每天保持更新。我只想将更新的帧保存到文件中,例如,直到昨天,一小时内创建的视频文件的帧数为 10460。
今天追加了新的10帧,我使用代码将最新的帧保存到文件中,如下所示,但是a轴太短,如何将x轴设置为与过去的max x相同?
anim = FuncAnimation(self.fig, self.anim_func, frames[10460:], init_func, interval=interval)
查看演示如何:
# Set up the figure and axis
fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(-1.5, 1.5)
影响动画效果:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
# Set up the figure and axis
fig, ax = plt.subplots()
ax.set_xlim(0, 10)
ax.set_ylim(-1.5, 1.5)
# Initialize the line plot
line, = ax.plot([], [], lw=2)
# Initial data
x = np.linspace(0, 10, 1000)
y = np.sin(x)
def init():
line.set_data([], [])
return line,
def update(frame):
# Adjust the right endpoint to shorten the wave from the right
x_new = np.linspace(0, 10 - frame / 10, 1000)
y_new = np.sin(x_new * np.pi) # Multiply by pi to maintain wave pattern despite shrinking domain
line.set_data(x_new, y_new)
return line,
# Create animation
ani = animation.FuncAnimation(fig, update, frames=np.linspace(0, 10, 100), init_func=init, blit=True)
plt.show()