微信扫一扫打赏支持

matplotlib_200730系列---14、Animation 动画

matplotlib_200730系列---14、Animation 动画

一、总结

一句话总结:

ani=animation.FuncAnimation(fig=fig,func=animate,frames=100,init_func=init,interval=20,blit=False)
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# Create a figure and a set of subplots.
fig,ax=plt.subplots()
x=np.arange(0,2*np.pi,0.01)
# print(x)
line,=ax.plot(x,np.sin(x))
# print(line)

# 动画
def animate(i): 
    line.set_ydata(np.sin(x+i/100))
    return line,

# 初始化页面
def init(): 
    line.set_ydata(np.sin(x))
    return line,

# frames=100 帧
# interval=20 ms
# blit=False 整张图全部更新blit的值就是false,只更新修改点值就是true
ani=animation.FuncAnimation(fig=fig,func=animate,frames=100,
                            init_func=init,interval=20,blit=False)

plt.show()

 

 

1、jupyter notebook matplotlib绘制动态图不能显示怎么办?

引入pylab库,加上%pylab就可以画出动态库了:Using matplotlib backend: Qt5Agg
from matplotlib import pylab
%pylab

Using matplotlib backend: Qt5Agg
Populating the interactive namespace from numpy and matplotlib

 

 

二、Animation 动画

博客对应课程的视频位置:

 

In [5]:
from matplotlib import pylab
# 加了这句话,即可在jupyter notebook中显示动态图
%pylab
Using matplotlib backend: Qt5Agg
Populating the interactive namespace from numpy and matplotlib
In [6]:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig,ax=plt.subplots()
x=np.arange(0,2*np.pi,0.01)
line,=ax.plot(x,np.sin(x))

def animate(i): 
    line.set_ydata(np.sin(x+i/100))
    return line,

def init(): 
    line.set_ydata(np.sin(x))
    return line,

ani=animation.FuncAnimation(fig=fig,func=animate,frames=100,
                            init_func=init,interval=20,blit=False)

plt.show()
In [ ]:
 

 

 

 
posted @ 2020-07-31 06:32  范仁义  阅读(300)  评论(0编辑  收藏  举报