【问题标题】:Matplotlib, how to animate a simple lineplot only onceMatplotlib,如何只为一个简单的线图设置一次动画
【发布时间】:2021-02-07 16:13:54
【问题描述】:

我有一个简单的线图,我只想制作一次动画,而不是像我当前的代码那样重复。我在 Stackoverflow 上浏览了一些解决方案(例如,Animate some divs only once),但它们似乎令人生畏或过于复杂。有没有简单的方法来做到这一点?另外,我们怎样才能提高速度?提前感谢您的宝贵时间。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation

df = pd.DataFrame({'x': {0: 0,
  1: 1,
  2: 2,
  3: 3,
  4: 4,
  5: 5,
  6: 6,
  7: 7,
  8: 8,
  9: 9,
  10: 10,
  11: 11,
  12: 12,
  13: 13,
  14: 14,
  15: 15,
  16: 16,
  17: 17,
  18: 18,
  19: 19,
  20: 20,
  21: 21,
  22: 22,
  23: 23,
  24: 24,
  25: 25,
  26: 26,
  27: 27,
  28: 28,
  29: 29,
  30: 30,
  31: 31,
  32: 32,
  33: 33,
  34: 34,
  35: 35,
  36: 36,
  37: 37,
  38: 38,
  39: 39,
  40: 40,
  41: 41,
  42: 42,
  43: 43,
  44: 44,
  45: 45,
  46: 46,
  47: 47,
  48: 48,
  49: 49,
  50: 50},
 'y': {0: 0.7695,
  1: 0.7983,
  2: 0.7958,
  3: 0.7975,
  4: 0.7983,
  5: 0.7966,
  6: 0.7971,
  7: 0.7962,
  8: 0.7962,
  9: 0.7975,
  10: 0.7983,
  11: 0.7987,
  12: 0.7996,
  13: 0.7992,
  14: 0.7967,
  15: 0.7983,
  16: 0.7971,
  17: 0.7987,
  18: 0.7979,
  19: 0.7983,
  20: 0.7983,
  21: 0.7921,
  22: 0.7975,
  23: 0.7962,
  24: 0.7975,
  25: 0.7979,
  26: 0.7983,
  27: 0.7992,
  28: 0.7983,
  29: 0.7983,
  30: 0.7987,
  31: 0.7983,
  32: 0.7983,
  33: 0.7983,
  34: 0.7992,
  35: 0.7975,
  36: 0.7996,
  37: 0.7992,
  38: 0.7979,
  39: 0.7987,
  40: 0.7983,
  41: 0.7983,
  42: 0.7987,
  43: 0.7987,
  44: 0.7992,
  45: 0.7992,
  46: 0.7979,
  47: 0.7996,
  48: 0.7992,
  49: 0.7987,
  50: 0.7992}})


x = df['x']
y = df['y']
fig, ax = plt.subplots()
line, = ax.plot(x, y, color='b')
def update(num, x, y, line):
    line.set_data(x[:num], y[:num])  
    return line,

ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line],
                              interval=15, blit=True)
ani.save('test.gif')
plt.show()

【问题讨论】:

    标签: matplotlib animation


    【解决方案1】:

    要控制重复,请设置repeat=False

    ani = animation.FuncAnimation(fig, update, len(x), fargs=[x, y, line], interval=15, blit=True, repeat=False)
    

    【讨论】:

    • 您好,感谢您抽出宝贵时间帮助我。但是,建议的解决方案 (repeat=False) 不起作用,因为保存的图形仍然重复动画。
    • 我觉得this comment & answer 很有帮助
    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    相关资源
    最近更新 更多