前言
matplotlib是python下非常好用的一个数据可视化套件,网上相关的教程也非常丰富,使用方便。本人需求一个根据实时数据刷新曲线的上位机软件,找了半天,基本上都是使用matplotlib的交互模式,我折腾半天还是没有实现想要的效果,但却通过另一种方法实现了想要的效果。
源码
注释已经很充分,不多赘述,直接看源码。
import matplotlib.pyplot as plt import numpy as np import threading import sys from random import random, randrange from time import sleep ''' 绘制2x2的画板 可设置窗口标题和4个子图标题 可更新曲线数据 ''' quit_flag = False # 退出标志 class Plot2_2(object): """ 2x2的画板 """ def __init__(self, wtitle='Figure', p1title='1', p2title='2', p3title='3', p4title='4'): self.sub_title = [p1title, p2title, p3title, p4title] # 4个子图的标题 self.fig, self.ax = plt.subplots(2, 2) # 创建2X2子图 self.fig.subplots_adjust(wspace=0.3, hspace=0.3) # 设置子图之间的间距 self.fig.canvas.set_window_title(wtitle) # 设置窗口标题 # 子图字典,key为子图的序号,value为子图句柄 self.axdict = {0: self.ax[0, 0], 1: self.ax[0, 1], 2: self.ax[1, 0], 3: self.ax[1, 1]} def showPlot(self): """ 显示曲线 """ plt.show() def setPlotStyle(self, index): """ 设置子图的样式,这里仅设置了标题 """ self.axdict[index].set_title(self.sub_title[index], fontsize=12) def updatePlot(self, index, x, y): """ 更新指定序号的子图 :param index: 子图序号 :param x: 横轴数据 :param y: 纵轴数据 :return: """ # X轴数据必须和Y轴数据长度一致 if len(x) != len(y): ex = ValueError("x and y must have same first dimension") raise ex self.axdict[index].cla() # 清空子图数据 self.axdict[index].plot(x, y) # 绘制最新的数据 self.setPlotStyle(index) # 设置子图样式 if min(x) < max(x): self.axdict[index].set_xlim(min(x), max(x)) # 根据X轴数据区间调整X轴范围 plt.draw() print("%s end" % sys._getframe().f_code.co_name) def updatePlot(plot): """ 模拟收到实时数据,更新曲线的操作 :param plot: 曲线实例 :return: """ print("Thread: %s" % threading.current_thread().getName()) count = 0 global quit_flag print("quit_flag[%s]" % str(quit_flag)) while True: if quit_flag: print("quit_flag[%s]" % str(quit_flag)) break count += 1 print("count#%d" % count) x = np.arange(0, 100, 1) y = np.random.normal(loc=1, scale=1, size=100) # 产生随机数,模拟变化的曲线 index = randrange(4) # 随机更新某一个子图 plot.updatePlot(index, x, y) sleep(random() * 3) def main(): p = Plot2_2() # 创建一个2X2画板 t = threading.Thread(target=updatePlot, args=(p,)) # 启动一个线程更新曲线数据 t.start() p.showPlot() # showPlot方法会阻塞当前线程,直到窗口关闭 print("plot close") global quit_flag quit_flag = True # 通知更新曲线数据的线程退出 t.join() print("Thread: %s end" % threading.current_thread().getName()) if __name__ == '__main__': main()
结语
上述方法初步实现了根据实时数据刷新曲线的效果,目前测试发现偶尔程序无法完全退出,还有待改进。到此这篇关于matplotlib实现数据实时刷新的示例代码的文章就介绍到这了,更多相关matplotlib 数据实时刷新内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 群星.1993-烧得厉害VOL.1好戏好歌唱不停【滚石】【WAV+CUE】
- 魔兽世界wlk阿奴巴拉克怎么打 wlk阿奴巴拉克机制与打法
- 归龙潮阿律的笛子位置在哪 阿律的笛子图文位置一览
- 归龙潮中浦城区福鼠位置在哪 中浦城区福鼠收集位置大全
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-9
- MatteoPasqualini-JohannSebastianBachItalienischesKonzertFranzsischeOuvertureVierDue
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]
- 《黑神话》纪录片9月20日上线!揭秘关卡为何删减
- 游戏科学估值已和育碧市值相当:后者有两万多名员工
- 黑神话第81周目Boss伤害有多恐怖?起手伤害直接秒
- 群星.2009-原班情歌2CD【SONY】【WAV+CUE】
- 群星.2024-长乐曲电视剧原声带【TME影音】【FLAC分轨】
- 群星.1993-烧得厉害VOL.1好戏好歌唱不停【滚石】【WAV+CUE】
- 那英《幸福的泪 磁性和爆发力完美展现 2CD》[WAV/分轨][600MB]
- 《宝丽金辉煌60年 女人篇3CD》[WAV+CUE][1.5G]