TensorFlow@IT·互联网程序员

matplotlib的基本用法(十六)——创建动画

2017-05-05  本文已影响50人  SnailTyan

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要介绍matplotlib的一些用法。

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


# 定义figure
fig, ax = plt.subplots()

# 定义数据
x = np.arange(0, 2 * np.pi, 0.01)
# line, 表示只取返回值中的第一个元素
line, = ax.plot(x, np.sin(x))

# 定义动画的更新
def update(i):
    line.set_ydata(np.sin(x + i/10))
    return line,

# 定义动画的初始值
def init():
    line.set_ydata(np.sin(x))
    return line,

# 创建动画
ani = animation.FuncAnimation(fig = fig, func = update, init_func = init, interval = 10, blit = False, frames = 200)

# 展示动画
plt.show()

# 动画保存
ani.save('sin.gif', writer = 'imagemagick', fps = 30, dpi = 100)
图像

创建下雨的动画:

图像
上一篇 下一篇

猜你喜欢

热点阅读