You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matplotlib中自定义X/Y轴并绘制紧凑的轮速折线图?

问题描述

我尝试绘制CSV文件中的数据,该文件数据格式如下:

time,odometry_right,odometry_left
1689366898638600587,0.31946682929992676,0.30494561791419983
1689366898688261032,0.28318527340888977,0.3129942715167999
1689366898736572980,0.25154152512550354,0.2811346650123596
1689366898787568807,0.2215549796819687,0.2806363105773926
1689366898836700439,0.23310235142707825,0.27680903673171997
1689366898887266635,0.28109779953956604,0.2958924174308777
1689366898936510562,0.26649942994117737,0.2813049554824829
1689366898987910985,0.2807117998600006,0.29548609256744385
1689366899036507368,0.2811105251312256,0.2811105251312256
1689366899087007522,0.2713738679885864,0.2856566905975342
1689366899136547327,0.2971850037574768,0.28232577443122864
1689366899187739372,0.2812372148036957,0.2812372148036957
1689366899236824035,0.2808254063129425,0.2808254063129425
1689366899286765813,0.2662796974182129,0.28107303380966187
1689366899337327003,0.25154393911361694,0.2663406431674957
1689366899390486478,0.2782253324985504,0.2928687632083893
1689366899437126636,0.2955184578895569,0.26596659421920776
1689366899487092256,0.27621591091156006,0.27621591091156006
1689366899536738872,0.28127074241638184,0.28127074241638184
1689366899587316513,0.24402768909931183,0.24402768909931183
1689366899636921882,0.2071893811225891,0.2515871226787567
1689366899686983346,0.2334650754928589,0.27723976969718933
1689366899736708879,0.25203293561935425,0.25203293561935425
1689366899788047075,0.26180514693260193,0.27634987235069275
1689366899838969707,0.24025367200374603,0.25526952743530273
1689366899887119293,0.23676078021526337,0.26635587215423584
1689366899937592983,0.2812446057796478,0.2664422392845154
1689366899987620115,0.266294926404953,0.25150078535079956
1689366900039707660,0.23341350257396698,0.23341350257396698
1689366900087186336,0.20715580880641937,0.20715580880641937
1689366900137516975,0.20662929117679596,0.1918700635433197
1689366900186663150,0.20016370713710785,0.18586629629135132
1689366900236943721,0.1777782291173935,0.19259308278560638
1689366900286716461,0.1753300130367279,0.1753300130367279
1689366900338431835,0.19237180054187775,0.19237180054187775
1689366900386737108,0.20711779594421387,0.19232365489006042
1689366900436511039,0.20705315470695496,0.20705315470695496

我的绘图代码如下:

right_vel = []
left_vel = []
time = []
 
with open('/home/autosys5/data.csv','r') as csvfile:
    lines = csv.reader(csvfile, delimiter=',')
    next(lines)
    for row in lines:
        time.append(row[0])
        right_vel.append(row[1])
        left_vel.append(row[2])
plt.plot(time,right_vel, label='Right Wheel Speed')
plt.plot(time,left_vel, label='Left Wheel Speed')

plt.xlabel('Time')
plt.ylabel('Speed')
plt.title('Wheel Speeds')
plt.legend()
plt.show()

当前生成的图表布局松散,我想要得到紧凑的折线图,同时需要了解如何在Matplotlib中自定义X、Y轴。

解决方案

1. 修复数据类型并生成相对时间

你的代码把时间和速度都存成了字符串,导致Matplotlib无法正确解析数值关系,且原始时间戳过大,X轴显示混乱。可以将时间转换为相对时间(从第一条数据开始的秒数),速度转换为浮点数:

import csv
import matplotlib.pyplot as plt

right_vel = []
left_vel = []
time = []
 
with open('/home/autosys5/data.csv','r') as csvfile:
    lines = csv.reader(csvfile, delimiter=',')
    next(lines)
    first_time = None
    for row in lines:
        # 将纳秒时间戳转为秒
        ts = int(row[0]) / 1e9
        if first_time is None:
            first_time = ts
        # 计算相对起始时间的差值
        time.append(ts - first_time)
        right_vel.append(float(row[1]))
        left_vel.append(float(row[2]))

plt.plot(time, right_vel, label='Right Wheel Speed')
plt.plot(time, left_vel, label='Left Wheel Speed')

plt.xlabel('Time (s)')
plt.ylabel('Speed')
plt.title('Wheel Speeds')
plt.legend()
plt.tight_layout()  # 自动调整布局,让图表更紧凑
plt.show()

2. 优化图表紧凑度的常用方法

  • 用plt.tight_layout():自动调整标题、标签、图例的间距,避免内容重叠,同时压缩冗余空间。
  • 自定义图表尺寸:通过plt.figure(figsize=(8,4))设置合适的宽高比,比如横向窄、纵向紧凑的尺寸。
  • 调整刻度密度:用plt.locator_params()减少轴刻度数量,避免拥挤:
    plt.locator_params(axis='x', nbins=5)
    

3. Matplotlib自定义X/Y轴的实用技巧

自定义轴显示范围

用plt.xlim()和plt.ylim()限定轴的显示区间,聚焦关键数据:

plt.xlim(0, 2)  # X轴只显示0-2秒的内容
plt.ylim(0.15, 0.35)  # Y轴只显示0.15-0.35的速度区间

自定义刻度与标签

用plt.xticks()和plt.yticks()手动设置刻度位置和显示文本:

# X轴在0、1、2秒位置显示自定义标签
plt.xticks([0,1,2], ['Start', '1s', '2s'])
# Y轴设置固定间隔的刻度
plt.yticks([0.2, 0.25, 0.3, 0.35])

格式化轴标签样式

通过FormatStrFormatter设置标签的数值格式:

from matplotlib.ticker import FormatStrFormatter
# X轴标签保留1位小数并添加单位
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%.1f s'))

旋转X轴刻度

避免长标签重叠:

plt.xticks(rotation=45)

内容的提问来源于stack exchange,提问作者icecream

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 02:54:52