判断网站做的好坏,男女做暖暖的试看网站酥酥影视,春节彩灯制作公司,网站 建设文档目录 一、前言二、实现过程2.1 封装属性2.2 数据流化显示2.3 输入数据的适应性 三、核心代码说明3.1 设置缓存3.2 随机信号3.3 根据设置绘图 五、总结四、源码PS.扩展阅读ps1.六自由度机器人相关文章资源ps2.四轴机器相关文章资源ps3.移动小车相关文章资源 一、前言
我们知道m… 目录 一、前言二、实现过程2.1 封装属性2.2 数据流化显示2.3 输入数据的适应性 三、核心代码说明3.1 设置缓存3.2 随机信号3.3 根据设置绘图 五、总结四、源码PS.扩展阅读ps1.六自由度机器人相关文章资源ps2.四轴机器相关文章资源ps3.移动小车相关文章资源 一、前言
我们知道matlab得工具箱用于分析数据非常强大可以通过模块化得方式对数据进行处理并可以有很多的工具模块最后可以加入一个窗口显示分析的结果。我们也知道python的matplot绘图也非常强大可以对各种维度的数据序列进行绘制还有python有丰富的生态有各行业的专业分析库但是python需要编代码对于工程人员使用起来有道门槛难以实现像matlab的低代码分析应用。 本系列文章试图打造一个python版的matlab把机器人和控制、信号处理、图像处理相关的模块封装成流程图的块并且打造跨进程通讯接口从而实现便捷的应用。 本篇基本实现了对matplot的封装。效果如下
二、实现过程
2.1 封装属性
matplot最常用的是曲线和散点、柱状图。我们把matplot封装到了一个叫MatplotPlotter的类里面然后只要把数据整理成固定的形式就可以调用这个类然后显示出曲线这个类把常用的属性给列出来了
class MatplotPlotter:def __init__(self, fignum,max_length50,maxframelen1024,viewwinlen20,plotlayout111,titletitle,xyzlabel[[X,Y,Z]], plottitles[plot,scatter,plot],xyzlim_max[],isrealtimeTrue,lineform[plot,scatter,plot,bar],lineform3D[plot3D,scatter3D,plot3D],colors[blue,red,green,yellow,pink,brown,orange,purple,cyan,gray ],size(5, 4),loc(0,0)):self.cache np.array([])self.max_length max_lengthself.max_frame_length maxframelenself.cache_length0 self.sizesizeself.loclocself.titletitleplt.style.use(dark_background)self.fig2D Noneself.ax2D Noneself.an2dNoneself.fig3D Noneself.ax3D Noneself.an3dNoneself.isshowFalseself.viewwinlenviewwinlenself.fignumfignumself.plotlayoutplotlayoutself.xyzlabelxyzlabelself.plottitlesplottitlesself.isrealtimeisrealtime#lineform:bar、plot、hist、pieself.lineformlineformself.lineform3Dlineform3Dself.formstyle{pie:self.ax2D.pie,plot:self.ax2D.plot,bar:self.ax2D.bar,scatter:self.ax2D.scatter,plot3D:self.ax3D.plot,scatter3D:self.ax3D.scatter}self.colorscolorsself.stepsNone2.2 数据流化显示
如上2D和3D的散点图、柱状图、线图都封装进去了。然后是图的title位置、大小的设置等。 还有个就是matplot一般很难显示动态图这里我们的图可以进行实时动态显示后期可以实时读取数据并显示在matplot中可查看实时趋势。
2.3 输入数据的适应性
这个模块可以接受numpy的数据通过读取numpy的shape类型来控制曲线的数量曲线的2D和3D显示如下 #只有y值1N或N,单曲线data0 np.array([1],dtypefloat) data00 np.array([[[1]]],dtypefloat) data1 np.array([[1,2]],dtypefloat) #有x、y值2N单曲线data2 np.array([[1], [2]],dtypefloat)#有x,y,z,3N),单曲线 data3 np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]],dtypefloat)#有x,y,(2,N,N),N根曲线data4 np.array([[[ 1.,1., 1.],],[[ 2.,2., 4.],]],dtypefloat)#有x,yz,(3,N,N),N跟曲线data5 np.array([[[ 1.,1., 1.],],[[ 1.,1., 1.],],[[ 2.,2., 4.],]],dtypefloat)
这样只要把数据进行预处理就可以马上进行可视化了。
三、核心代码说明
3.1 设置缓存
设置一个缓存可以持续接收数据流并显示出来同时如果超过缓存允许的长度就把最早的数据舍掉加入最新的数据。这里涉及到对输入data的shape的判断对支持的shape进行解析要考虑经可能多的shape存在。这个需要经过多种shape的输入测试。 然后如果输入只有一维也就是只有Y值得情况就应该补上x得值这里x得值用step得自然数补充。 同时如果缓存中存储了数据再输入数据时需要经过numpy得拼接等操作。 def add_data(self, data): if data.shape[0]0 and data.shape[0]3: if len(data.shape)3:return Falseelse:if len(data.shape)1:#print(add data fail,len(data.shape) must at least 1)datadata.reshape(len(data),1)if len(data.shape)2:if data.shape[0]1:numlen(data[0])if self.steps is None:x np.arange(num) else:x self.stepsnum data np.concatenate((x.reshape(1,num), data), axis0)self.stepsx#print(self.steps,self.steps)data data.reshape(data.shape[0], data.shape[1],1)if len(data.shape)3 and data.shape[0]1:passelse:#print(add new data)if len(self.cache.shape) len(data.shape):pass else:return Falsereturn Trueelse:#single curveprint(single curve)return Falseelse:print(data.shape[0] out of range must be (0,3])return False3.2 随机信号
为了测试趋势曲线图可以设置一个随机信号产生得模块形成信号源实际应用可以采集传感器中得信号来冲当信号源。
def draw(plot,data):# 定义均值和标准差mu 5sigma 0.6 print(plot.add_data(data))if data.shape[0]1:if len(data.shape)1:passnumlen(data)errornp.random.normal(mu, sigma, num)data[0]error[0]elif len(data.shape)3:numdata.shape[1]errornp.random.normal(mu, sigma, num).reshape(num,1)#error2 np.random.normal(mu, sigma, num)for i in range(1,data.shape[2]):error1 np.random.normal(mu, sigma, num).reshape(num,1)#error2 np.random.normal(mu, sigma, num)errornp.concatenate((error1.reshape(num,1), error), axis1)data[0]errorprint(data)else:numlen(data[0])errornp.random.normal(mu, sigma, num)data[0]error elif data.shape[0]2: data[0]data[0](data.shape[1]) numlen(data[1])if len(data.shape)3:#2D mutpleerrornp.random.normal(mu, sigma, num).reshape(num,1)#error2 np.random.normal(mu, sigma, num)for i in range(1,data.shape[2]):error1 np.random.normal(mu, sigma, num).reshape(num,1)#error2 np.random.normal(mu, sigma, num)errornp.concatenate((error1.reshape(num,1), error), axis1)data[1]error#print(data4:,data4[0])elif len(data.shape)2:#2Derrornp.random.normal(mu, sigma, num).reshape(num,1)data[1]errorelif data.shape[0]3:data[0]data[0](data.shape[1]) data[1]data[1](data.shape[1])numlen(data[2])if len(data.shape)3:#3D multerrornp.random.normal(mu, sigma, num).reshape(num,1)#error2 np.random.normal(mu, sigma, num)for i in range(1,data.shape[2]):error1 np.random.normal(mu, sigma, num).reshape(num,1)#error2 np.random.normal(mu, sigma, num)errornp.concatenate((error1.reshape(num,1), error), axis1)data[2]errorelif len(data.shape)2:#3D singleerrornp.random.normal(mu, sigma, num)data[2]errorplot.plot()3.3 根据设置绘图
当存入缓存后绘图得任务就是解析缓存中得数据把数据根据颜色、标题、坐标最大最小、绘制得类型等绘制出来。 def plot(self):number_str str(self.plotlayout) if len(number_str) 3: hundreds int(number_str[0])tens int(number_str[1])units int(number_str[2])if hundreds*tens ! units:return Falseif len(self.cache.shape)3:if self.plotlayout%10 ! self.cache.shape[2] and self.plotlayout%10 ! 1:return Falseif len(self.cache.shape)2:#single lineself.cacheself.cache.reshape(self.cache.shape[0],self.cache.shape[1],1)if units1: if self.cache.shape[0] 2: # 1D data if self.fig2D None: self.fig2D plt.figure(self.fignum,figsizeself.size)self.fig2D.canvas.manager.window.move(self.loc[0],self.loc[1])if self.ax2D is None:#nrows, ncols, index self.ax2D self.fig2D.add_subplot(111) self.ax2D.grid(linestyle--, alpha0.5)self.ax2D.set_xlabel(X)self.ax2D.set_ylabel(Y)self.ax2D.set_title(self.title) if self.an2d is not None:#self.an2d.pop().remove()#print(len(self.an2d))if isinstance(self.an2d, matplotlib.container.BarContainer):self.an2d.remove()else: self.an2d.pop().remove()maxxfloat(-inf)minxfloat(inf)for line in range(self.cache.shape[2]): x self.cache[0,:,line]y self.cache[1,:,line]if len(x)self.viewwinlen:xx[-self.viewwinlen:]yy[-self.viewwinlen:]pass五、总结
实现了这个模块后就可以封装到工作流中可以供以后数据分析监视使用。有了这个块就可以绘制多个图在屏幕得任意位置便于数据分析和实时监控特别适合试验室监视。 当然可以看出matplot这个画布如果用于实时显示可以看见开销和实时性较大。用于研究和原型开发足够但是用于实时得生产环境还时考虑进行优化。
四、源码
已经上传到资源下载链接
[------------本篇完--------------------------]
PS.扩展阅读
————————————————————————————————————————
对于python机器人编程感兴趣的小伙伴可以进入如下链接阅读相关咨询
ps1.六自由度机器人相关文章资源
(1) 对六自由度机械臂的运动控制及python实现附源码)
(2) N轴机械臂的MDH正向建模及python算法
ps2.四轴机器相关文章资源
(1) 文章python机器人编程——用python实现一个写字机器人 2python机器人实战——0到1创建一个自动是色块机器人项目-CSDN直播
(3)博文《我从0开始搭建了一个色块自动抓取机器人并实现了大模型的接入和语音控制-(上基础篇)》的vrep基础环境 (3)博文《我从0开始搭建了一个色块自动抓取机器人并实现了大模型的接入和语音控制-(上基础篇)》的vrep基础环境 (4)实现了语音输入大模型指令解析机器视觉机械臂流程打通
ps3.移动小车相关文章资源
1python做了一个极简的栅格地图行走机器人到底能干啥[第五弹]——解锁蒙特卡洛定位功能-CSDN博客 (2) 对应python资源源码地址
(3)python机器人编程——差速AGV机器、基于视觉和预测控制的循迹、自动行驶上篇_agv编程-CSDN博客 (4)python机器人编程——差速AGV机器、基于视觉和预测控制的循迹、自动行驶下篇_agv路线规划原则python-CSDN博客 对应python及仿真环境资源源码链接