欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python学习之使用Matplotlib画实时的动态折线图的示例代码

时间:2021-04-15 11:17:26|栏目:Python代码|点击:

有时,为了方便看数据的变化情况,需要画一个动态图来看整体的变化情况。主要就是用Matplotlib库。

首先,说明plot函数的说明。

plt.plot(x,y,format_string,**kwargs) 

x是x轴数据,y是y轴数据。x与y维度一定要对应。

format_string控制曲线的格式字串

下面详细说明:

  • color(c):线条颜色
  • linestyle(ls):线条样式
  • linewidth(lw):线的粗细

关于标记的一些参数:

  • marker:标记样式
  • markeredgecolor(mec):标记边缘颜色
  • markeredgewidth(mew):标记边缘宽度
  • markerfacecolor(mfc):标记中心颜色
  • markersize(ms):标记大小

另外,marker关键字参数可以和color以及linestyle这两个关键字参数合并为一个字符串。
例如:‘ro-'表示红色的直线,标记为圆形

线条color颜色:

线条样式(linestyle):

标记(marker)参数:

程序demo如下:

得到的结果是循环的sin(x)的折线图

'''
动态折线图演示示例
'''
 
import numpy as np
import matplotlib.pyplot as plt
 
plt.ion()
plt.figure(1)
t_list = []
result_list = []
t = 0
 
while True:
 if t >= 10 * np.pi:
  plt.clf()
  t = 0
  t_list.clear()
  result_list.clear()
 else:
  t += np.pi / 4
  t_list.append(t)
  result_list.append(np.sin(t))
  plt.plot(t_list, result_list,c='r',ls='-', marker='o', mec='b',mfc='w') ## 保存历史数据
  #plt.plot(t, np.sin(t), 'o')
  plt.pause(0.1)

得到的结果如下:

参考博客链接:https://blog.csdn.net/zhanghao3389/article/details/82685072

https://blog.csdn.net/u013468614/article/details/58689735

上一篇:Python制作一个仿QQ办公版的图形登录界面

栏    目:Python代码

下一篇:Python的Flask框架及Nginx实现静态文件访问限制功能

本文标题:python学习之使用Matplotlib画实时的动态折线图的示例代码

本文地址:http://www.codeinn.net/misctech/101638.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有