标签:set frame matplotlib animation 绘图 ax x1
基于matplotlib.animation的动态绘图方法
matplotlib是python中最基本,也是最常用的画图工具。利用matplotlib不仅可以绘制各种各样的图片,还可以制作一些小动画,下面就来介绍一下matplotlib如何制作动画。
动画的制作是基于matplotlib的animation,对于一条线的绘制,网上的教程很多,这里不介绍,主要介绍一下一图多线的情况。
# 导入相关库
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# 生成数据
x1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y2 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10]
# 简单设置一下绘图基本参数
sns.set_style('darkgrid')
# 绘图
fig, ax = plt.subplots()
ax.set_title('draw lines')
ax.set_xlabel('x')
ax.set_ylabel('y')
ln1, =ax.plot([], [], 'r-o', animated=False)
ln2, =ax.plot([], [], 'r-o', animated=False)
ax.legend(['y1, y2'], loc='upper left')
def update(frame):
x_1 = x1[0:frame]
y_1 = x1[0:frame]
x_2 = x1[0:frame]
y_2 = x1[0:frame]
ln1.set_data(x_1, y_1)
ln2.set_data(x_2, y_2)
return ln1, ln2,
ani = animation.FuncAnimation(fig, update, frames=len(x1)+1, interval=300, blit=True)
plt.show()
绘制的图像如下:
标签:set,frame,matplotlib,animation,绘图,ax,x1 来源: https://www.cnblogs.com/littletom/p/15828337.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。