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

如何避免matplotlib绘图时自动简化Y轴刻度?

解决Matplotlib Y轴刻度自动偏移/科学计数法的问题
  • 直接用ticklabel_format()函数关闭Y轴的自动偏移和科学计数法就行:
    如果你用pyplot基础接口,代码可以这么写:

    import matplotlib.pyplot as plt
    
    # 替换成你从BMP280采集到的压力数据
    pressure_data = [996.7, 996.8, 996.9, 997.0, 997.1]
    sample_time = [1, 2, 3, 4, 5]
    
    plt.plot(sample_time, pressure_data)
    # 仅针对Y轴关闭偏移和科学计数法
    plt.ticklabel_format(style='plain', useOffset=False, axis='y')
    plt.ylabel('压力 (hPa)')
    plt.xlabel('采样次数')
    plt.show()
    
  • 要是习惯用Matplotlib的面向对象写法(更灵活,适合复杂绘图),可以这么写:

    import matplotlib.pyplot as plt
    
    pressure_data = [996.7, 996.8, 996.9, 997.0, 997.1]
    sample_time = [1, 2, 3, 4, 5]
    
    fig, ax = plt.subplots()
    ax.plot(sample_time, pressure_data)
    ax.ticklabel_format(style='plain', useOffset=False, axis='y')
    ax.set_ylabel('压力 (hPa)')
    ax.set_xlabel('采样次数')
    plt.show()
    

参数说明:

  • style='plain':强制用普通数字格式显示刻度,不会触发科学计数法
  • useOffset=False:禁止Matplotlib自动添加偏移量(就是你图里的+9.967e2)
  • axis='y':只对Y轴生效,改成axis='both'可以同时处理X轴

这种自动偏移是Matplotlib的默认行为,因为你的压力数据数值区间很窄、数值接近,它想简化刻度显示,关掉这个设置就能显示完整数值了。

内容的提问来源于stack exchange,提问作者ALBERTUS STEVEN SANTOSA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 20:15:30