基于matplotlib.animation的动态绘图方法
2022/1/20 23:21:00
本文主要是介绍基于matplotlib.animation的动态绘图方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
基于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()
绘制的图像如下:
这篇关于基于matplotlib.animation的动态绘图方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28微服务架构中API版本控制的实践
- 2024-09-28AI给的和自己写的Python代码,都无法改变输入框的内容,替换也不行
- 2024-09-27Sentinel配置限流资料:新手入门教程
- 2024-09-27Sentinel配置限流资料详解
- 2024-09-27Sentinel限流资料:新手入门教程
- 2024-09-26Sentinel限流资料入门详解
- 2024-09-26Springboot框架资料:初学者入门教程
- 2024-09-26Springboot框架资料详解:新手入门教程
- 2024-09-26Springboot企业级开发资料:新手入门指南
- 2024-09-26SpringBoot企业级开发资料新手指南