使用matplotlib怎么实现一个数据实时刷新功能
创始人
2024-12-28 00:41:03
0次
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()

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...