欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 数据分析——Python绘制实时的动态折线图

数据分析——Python绘制实时的动态折线图

2026/1/31 7:48:50 来源:https://blog.csdn.net/qq_45445740/article/details/143822719  浏览:    关键词:数据分析——Python绘制实时的动态折线图

最近在做视觉应用开发,有个需求需要实时获取当前识别到的位姿点位是否有突变,从而确认是否是视觉算法的问题,发现Python的Matplotlib进行绘制比较方便。

目录

  • 1.数据绘制
  • 2.绘制实时的动态折线图
  • 3.保存实时数据到CSV文件中

import matplotlib.pyplot as plt
import random
import numpy as np
import time
import os
import csv

1.数据绘制

def draw_data():index = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]x_data = [1, 0.2, 0.3, 4, 0.5, 0.6, 1, 0.8, 0.9, -1]# 创建折线图plt.plot(index, x_data, marker='o', color='b', linestyle='-', label='x_data')# 设置标题和标签plt.title("x_data")plt.xlabel("Index")plt.ylabel("X Data")# 显示图例plt.legend()# 设置横坐标刻度,使得每个index值都显示plt.xticks(index)# 显示图形plt.show()

在这里插入图片描述

2.绘制实时的动态折线图

虽然可以实时绘制,但会不断新增新的窗口,导致越到后面越卡顿,后面采用了保存到CSV文件进行分析的方法。

def realtime_data_draw():'''动态折线图实时绘制'''plt.ion()plt.figure(1)t_list = []result_list = []t = 0while True:if t >= 100 * np.pi:plt.clf()t = 0t_list.clear()result_list.clear()else:t += np.pi / 4t_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)

在这里插入图片描述

3.保存实时数据到CSV文件中

将实时的数据保存到CSV文件中,通过excel文件绘制折线图进行分析。

def realtime_data_save_csv():# 模拟实时生成的轨迹点坐标count = 0# CSV 文件路径file_path = 'vision_data/pose.csv'if os.path.exists(file_path):os.remove(file_path)# 写入表头并开始写入数据with open(file_path, mode='w', newline='') as file:writer = csv.writer(file)# 写入表头writer.writerow(['Index', 'X', 'Y', 'Z', 'RX', 'RY', 'RZ'])while True:count += 1x_value = random.uniform(-0.5, 0.5)y_value = random.uniform(-0.5, 0.5)z_value = random.uniform(-0.1, 0.8)rx_value = random.uniform(-3.14, 3.14)ry_value = random.uniform(-3.14, 3.14)rz_value = random.uniform(-3.14, 3.14)# 将生成的数据写入 CSV 文件writer.writerow([count, x_value, y_value, z_value, rx_value, ry_value, rz_value])time.sleep(0.05)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com