一 代码
参考:官网API-Example
'''
mathplotlib-Animation2:
1。随机描点
2。图像动画效果
'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import cv2 as cv
def update_line(num,data,line):
line.set_data(data[..., :num])
re = [line]#返回数据必须为一个可iterable的数据,或者改成'return line,'
return re
fig1 = plt.figure()
data = np.random.rand(2,25)
ll,= plt.plot([],[],'r-')
#设置范围
plt.xlim(0,1)
plt.ylim(0,1)
plt.xlabel('x')
plt.title('test')
line_ani = animation.FuncAnimation(fig1,update_line,25,fargs=(data,ll),interval=50, blit=True)
fig2 = plt.figure()
##img = cv.imread('E:\\fruits.jpg',0)#取0通道
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)#两边求三角形斜边值sqrt(x*x + y*y)。
ims = []
for add in np.arange(15):
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
blit=True)
plt.show()
##r如果是对指定的图片进行变换动画效果 fig2 = plt.figure() img = cv.imread('E:\\fruits.jpg',0)#取0通道 len = img.shape x = np.arange(0,len[0]) y = np.arange(0,len[1]).reshape(-1, 1) base = np.hypot(x, y)#两边求三角形斜边值sqrt(x*x + y*y)。耗内存耗时间 ims = [] for add in np.arange(2): ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))#这里将很耗内存 im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000, blit=True) plt.show()
二 效果