python中matplotlib保存一些帧到文件时如何设置xlim?

问题描述 投票:0回答:1

我使用python和matplotlib创建条形图竞赛动画,数据是时间序列并且每天保持更新。我只想将更新的帧保存到文件中,例如,直到昨天,一小时内创建的视频文件的帧数为 10460。 enter image description here

今天追加了新的10帧,我使用代码将最新的帧保存到文件中,如下所示,但是a轴太短,如何将x轴设置为与过去的max x相同?

anim = FuncAnimation(self.fig, self.anim_func, frames[10460:], init_func, interval=interval)

enter image description here

python matplotlib-animation
1个回答
0
投票

查看演示如何:

# 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()
© www.soinside.com 2019 - 2024. All rights reserved.