【问题标题】:Putting limits on a matplotlib animation with plt.ion()使用 plt.ion() 限制 matplotlib 动画
【发布时间】:2021-04-03 18:30:52
【问题描述】:

我正在尝试使用 matplotlib 不使用 FuncAnimation 制作 动画,因为它会导致 HTML (ffmpeg) 出现问题。我现在正在尝试使用更常用于实况剧情的plt.ion()

我制作动画没有问题,但曲线并没有真正移动,大部分时间是边界在移动。

我尝试了通常的界限,比如 set plt.xlim(#boundaries) 和 for the y... 但它没有用。关于如何解决问题的任何线索?

这是我所做的(这只是示例代码,因为我想做的动画是量子波函数,而那个代码已经有点乱了)

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

a = int(input("render level = "))
tmax = float(input("tmax = "))

x = np.linspace(0, 2*np.pi, a)
t = np.linspace(0, tmax, a)

plt.figure()
plt.ion()
plt.xlim([0, 2*np.pi])
plt.ylim([-1.2, 1.2])
plt.plot([], [])

i = 0


while i<a:
    y = np.cos(t[i])*np.sin(x)
    print(t[i])

    plt.clf()
    plt.plot(x, y)
    plt.pause(1/(a*tmax))
    i = i + 1

【问题讨论】:

    标签: python matplotlib boundaries


    【解决方案1】:

    它不只是绘制边界。它给人的印象是因为 y 轴限制一直在自我调整。这是因为您正在清除 while 循环中的图形,并且它会忘记您之前设置的任何内容。

    因此,您必须在 while 循环中重复轴限制。尝试像这样编写您的 while 循环:

    while i<a:
        y = np.cos(t[i])*np.sin(x)
        print(t[i])
    
        plt.clf()
        plt.plot(x, y)
        plt.xlim([0, 2*np.pi])
        plt.ylim([-1.2, 1.2])
        plt.title('Time is {}'.format(i))
        plt.pause(1/(a*tmax))
    
        i = i + 1
    

    【讨论】:

    • @nobody48sheldor 如果您没有更多问题,请考虑接受此答案,以免它在主站点列表上显示为“活动”。
    猜你喜欢
    • 2022-01-22
    • 2016-11-02
    • 2019-04-24
    • 1970-01-01
    • 2023-03-13
    • 2017-06-21
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多