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

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

目录

python 复制代码
import matplotlib.pyplot as plt
import random
import numpy as np
import time
import os
import csv

1.数据绘制

python 复制代码
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文件进行分析的方法。

python 复制代码
def realtime_data_draw():
    '''
    动态折线图实时绘制
    '''
    plt.ion()
    plt.figure(1)
    t_list = []
    result_list = []
    t = 0

    while True:
        if t >= 100 * np.pi:
            plt.clf()
            t = 0
            t_list.clear()
            result_list.clear()
        else:
            t += np.pi / 4
            t_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文件绘制折线图进行分析。

python 复制代码
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 += 1
            x_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)



相关推荐
桃西西呀19 分钟前
国内金价破 1000,「涨了多少」和「该买多少」是数学问题
人工智能·python·数据分析
张人玉23 分钟前
茶树 NeRF(Neural Radiance Fields,神经辐射场)三维重建与数据分析可视化系统——基于神经辐射场的茶树三维重建方法实现可视化大屏
python·数据挖掘·数据分析·echarts·three.js
脉动数据行情126 分钟前
Python WebSocket 国内商品期货|螺纹钢 & 铁矿石实时行情监听
开发语言·python·websocket
circuitsosk1 小时前
智能体任务拆解与执行:基于ReAct+Plan-and-Execute框架的行业Skill构建实录
前端·javascript·python·react.js·react·llm agent·智能体编排
麻雀飞吧1 小时前
近期量化工具怎么选,先看你卡在哪一环
人工智能·python
通信仿真爱好者1 小时前
第【109】期--基于神经网络的OFDM峰均功率比降低方法--python完整代码
python·神经网络·ofdm·限幅滤波·峰均功率比·papr降低
阿图灵1 小时前
OpenCV 图像特征与匹配:SIFT 特征检测与 BFMatcher 暴力匹配
图像处理·人工智能·python·opencv·计算机视觉·sift
Mr数据杨1 小时前
商店商品销量预测助力库存优化
人工智能·数据分析·kaggle竞赛
王志来137944730081 小时前
工控服务器机箱选型决策要素解析:匀天以“快全准省”构建价值坐标
运维·服务器·python·devops
阿图灵1 小时前
OpenCV 实用技巧三连:视频转图片、图片转视频、Pillow 与 OpenCV 互转
图像处理·python·opencv·计算机视觉·音视频·pillow