Python matplotlib实时画图案例


Posted in Python onApril 23, 2020

实时画图

import matplotlib.pyplot as plt

ax = []   # 定义一个 x 轴的空列表用来接收动态的数据
ay = []   # 定义一个 y 轴的空列表用来接收动态的数据
plt.ion()   # 开启一个画图的窗口
for i in range(100): # 遍历0-99的值
 ax.append(i)  # 添加 i 到 x 轴的数据中
 ay.append(i**2) # 添加 i 的平方到 y 轴的数据中
 plt.clf()  # 清除之前画的图
 plt.plot(ax,ay) # 画出当前 ax 列表和 ay 列表中的值的图形
 plt.pause(0.1)  # 暂停一秒
 plt.ioff()  # 关闭画图的窗口

实时画图 效果图

Python matplotlib实时画图案例

补充知识:Python 绘图与可视化 matplotlib 动态条形图 bar

第一种办法

一种方法是每次都重新画,包括清除figure

def animate(fi):
 bars=[]
 if len(frames)>fi:
  # axs.text(0.1,0.90,time_template%(time.time()-start_time),transform=axs.transAxes)#所以这样
  time_text.set_text(time_template%(0.1*fi))#这个必须没有axs.cla()才行
  # axs.cla()
  axs.set_title('bubble_sort_visualization')
  axs.set_xticks([])
  axs.set_yticks([])
  bars=axs.bar(list(range(Data.data_count)),#个数
    [d.value for d in frames[fi]],#数据
    1,    #宽度
    color=[d.color for d in frames[fi]]#颜色
    ).get_children()
 return bars
 anim=animation.FuncAnimation(fig,animate,frames=len(frames), interval=frame_interval,repeat=False)

这样效率很低,而且也有一些不可取的弊端,比如每次都需要重新设置xticks、假如figure上添加的有其他东西,这些东西也一并被clear了,还需要重新添加,比如text,或者labale。

第二种办法

可以像平时画线更新data那样来更新bar的高

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation
 
 
fig=plt.figure(1,figsize=(4,3))
ax=fig.add_subplot(111)
ax.set_title('bar_animate_test')
#ax.set_xticks([])注释了这个是能看到变化,要不看不到变化,不对,能看到变化,去了注释吧
#ax.set_yticks([])
ax.set_xlabel('xlable')
N=5
frames=50
x=np.arange(1,N+1)
 
collection=[]
collection.append([i for i in x])
for i in range(frames):
 collection.append([ci+1 for ci in collection[i]])
print(collection)
xstd=[0,1,2,3,4]
bars=ax.bar(x,collection[0],0.30)
def animate(fi):
 # collection=[i+1 for i in x]
 ax.set_ylim(0,max(collection[fi])+3)#对于问题3,添加了这个
 for rect ,yi in zip(bars,collection[fi]):
 rect.set_height(yi)
 # bars.set_height(collection)
 return bars
anim=animation.FuncAnimation(fig,animate,frames=frames,interval=10,repeat=False)
plt.show()

问题

*)TypeError: ‘numpy.int32' object is not iterable

x=np.arange(1,N+1)<br>collection=[i for i in x]
#collection=[i for i in list(x)]#错误的认为是dtype的原因,将这里改成了list(x)
for i in range(frames):
 collection.append([ci+1 for ci in collection[i]])#问题的原因是因为此时的collection还是一个一位数组,所以这个collection[i]是一个x里的一个数,并不是一个列表,我竟然还以为的dtype的原因,又改了
xstd=[0,1,2,3,4]

应该是

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
collection=[]
collection.append([i for i in x])#成为二维数组
for i in range(frames):
 collection.append([ci+1 for ci in collection[i]])

然后又出现了下面的问题:

*)TypeError: only size-1 arrays can be converted to Python scalars

Traceback (most recent call last):
 File "forTest.py", line 22, in <module>
 bars=ax.bar(x,collection,0.30)
 File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\__init__.py", line 1589, in inner
 return func(ax, *map(sanitize_sequence, args), **kwargs)
 File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar
 label='_nolegend_',
 File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 707, in __init__
 Patch.__init__(self, **kwargs)
 File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 89, in __init__
 self.set_linewidth(linewidth)
 File "C:\Users\Administrator.SC-201605202132\Envs\sort\lib\site-packages\matplotlib\patches.py", line 368, in set_linewidth
 self._linewidth = float(w)
TypeError: only size-1 arrays can be converted to Python scalars

应该是传递的参数错误,仔细想了一下,在报错的代码行中,collection原来是没错的,因为原来是一维数组,现在变成二维了,改为

bars=ax.bar(x,collection[0],0.30)

好了

*)出现的问题,在上面的代码中,运行的时候不会画布的大小不会变,会又条形图溢出的情况,在animate()中添加了

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def animate(fi):
 # collection=[i+1 for i in x]
 ax.set_ylim(0,max(collection[fi])+3)#添加了这个
 for rect ,yi in zip(bars,collection[fi]):
 rect.set_height(yi)
 
 # bars.set_height(collection)
 return bars

别的属性

*)条形图是怎样控制间隔的:

是通过控制宽度

width=1,#没有间隔,每个条形图会紧挨着

*)errorbar:

是加一个横线,能通过xerr和yerr来调整方向

Python matplotlib实时画图案例

xstd=[0,1,2,3,4]
bars=ax.bar(x,collection,0.30,xerr=xstd)

以上这篇Python matplotlib实时画图案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python标准库与第三方库详解
Jul 22 Python
在Python的Flask中使用WTForms表单框架的基础教程
Jun 07 Python
python使用KNN算法手写体识别
Feb 01 Python
VSCode下好用的Python插件及配置
Apr 06 Python
Python文件打开方式实例详解【a、a+、r+、w+区别】
Mar 30 Python
Python matplotlib画图与中文设置操作实例分析
Apr 23 Python
Django的models中on_delete参数详解
Jul 16 Python
pycharm 安装JPype的教程
Aug 08 Python
python实现的读取网页并分词功能示例
Oct 29 Python
简单了解Python多态与属性运行原理
Jun 15 Python
python 模拟登陆163邮箱
Dec 15 Python
LyScript实现绕过反调试保护的示例详解
Aug 14 Python
windows下的pycharm安装及其设置中文菜单
Apr 23 #Python
使用python+poco+夜神模拟器进行自动化测试实例
Apr 23 #Python
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
Apr 23 #Python
python+adb命令实现自动刷视频脚本案例
Apr 23 #Python
python+adb+monkey实现Rom稳定性测试详解
Apr 23 #Python
通过python调用adb命令对App进行性能测试方式
Apr 23 #Python
python 将视频 通过视频帧转换成时间实例
Apr 23 #Python
You might like
Windows下的PHP5.0安装配制详解
2006/09/05 PHP
php获取textarea的值并处理回车换行的方法
2014/10/20 PHP
php动态添加url查询参数的方法
2015/04/14 PHP
php上传功能集后缀名判断和随机命名(强力推荐)
2015/09/10 PHP
PHP中字符与字节的区别及字符串与字节转换示例
2016/10/15 PHP
PHP基于接口技术实现简单的多态应用完整实例
2017/04/26 PHP
锋利的jQuery 要点归纳(三) jQuery中的事件和动画(上:事件篇)
2010/03/24 Javascript
ExtJS 下拉多选框lovcombo
2010/05/19 Javascript
Extjs3.0 checkboxGroup 动态添加item实现思路
2013/08/14 Javascript
Firefox和IE兼容性问题及解决方法总结
2013/10/08 Javascript
jQuery实现的在线答题功能
2015/04/12 Javascript
javascript学习笔记之函数定义
2015/06/25 Javascript
基于JS代码实现导航条弹出式悬浮菜单
2016/06/17 Javascript
终于实现了!精彩的jquery弹幕效果
2016/07/18 Javascript
JS识别浏览器类型(电脑浏览器和手机浏览器)
2016/11/18 Javascript
js遮罩效果制作弹出注册界面效果
2017/01/25 Javascript
禁止弹窗中蒙层底部页面跟随滚动的几种方法
2017/12/07 Javascript
一秒学会微信小程序制作table表格
2019/02/14 Javascript
微信小程序BindTap快速连续点击目标页面跳转多次问题处理
2019/04/08 Javascript
Vue实现base64编码图片间的切换功能
2019/12/04 Javascript
vue treeselect获取当前选中项的label实例
2020/08/31 Javascript
JavaScript实现商品评价五星好评
2020/11/30 Javascript
[01:19]2014DOTA2国际邀请赛 采访TITAN战队ohaiyo 能赢DK很幸运
2014/07/12 DOTA
[43:36]Liquid vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python脚本作为Windows服务启动代码详解
2018/02/11 Python
python实现音乐下载器
2018/04/15 Python
简单易懂Pytorch实战实例VGG深度网络
2019/08/27 Python
让你的Python代码实现类型提示功能
2019/11/19 Python
如何用Matplotlib 画三维图的示例代码
2020/07/28 Python
应届生煤化工求职信
2013/10/21 职场文书
党风廉政承诺书
2014/03/27 职场文书
寄语学生的话
2014/04/10 职场文书
关于Vue Router的10条高级技巧总结
2021/05/06 Vue.js
能让Python提速超40倍的神器Cython详解
2021/06/24 Python
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏
彻底卸载VMware虚拟机的超详细步骤记录
2022/07/15 Servers