forked from Snailclimb/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo19.py
More file actions
35 lines (27 loc) · 718 Bytes
/
demo19.py
File metadata and controls
35 lines (27 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
"""
动画
"""
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.html', writer = 'imagemagick', fps = 30, dpi = 100)