mirror of
https://github.com/Estom/notes.git
synced 2026-04-13 18:00:27 +08:00
matplotlib & pandas
This commit is contained in:
45
Python/matplotlab/gallery/animation/simple_anim.md
Normal file
45
Python/matplotlab/gallery/animation/simple_anim.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# 动画线图
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.animation as animation
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
x = np.arange(0, 2*np.pi, 0.01)
|
||||
line, = ax.plot(x, np.sin(x))
|
||||
|
||||
|
||||
def init(): # only required for blitting to give a clean slate.
|
||||
line.set_ydata([np.nan] * len(x))
|
||||
return line,
|
||||
|
||||
|
||||
def animate(i):
|
||||
line.set_ydata(np.sin(x + i / 100)) # update the data.
|
||||
return line,
|
||||
|
||||
|
||||
ani = animation.FuncAnimation(
|
||||
fig, animate, init_func=init, interval=2, blit=True, save_count=50)
|
||||
|
||||
# To save the animation, use e.g.
|
||||
#
|
||||
# ani.save("movie.mp4")
|
||||
#
|
||||
# or
|
||||
#
|
||||
# from matplotlib.animation import FFMpegWriter
|
||||
# writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
|
||||
# ani.save("movie.mp4", writer=writer)
|
||||
|
||||
plt.show()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: simple_anim.py](https://matplotlib.org/_downloads/simple_anim.py)
|
||||
- [下载Jupyter notebook: simple_anim.ipynb](https://matplotlib.org/_downloads/simple_anim.ipynb)
|
||||
Reference in New Issue
Block a user