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

如何调整Matplotlib绘图坐标轴的数值小数位数与显示范围

坐标轴小数位调整方案

你可以通过matplotlib的刻度格式化工具修改Y轴的数值显示规则,修改后的完整可运行代码如下:

# 导入刻度格式化工具
from matplotlib.ticker import FormatStrFormatter

acc = history.history['accuracy']
acc_val = history.history['val_accuracy']
epochs = range(len(acc))

# 原代码缺少曲线绘制逻辑,此处补充
plt.plot(epochs, acc, label='训练准确率')
plt.plot(epochs, acc_val, label='验证准确率')

plt.title('训练与验证准确率变化')
plt.xlabel('迭代轮次(Epochs)')
plt.ylabel('准确率(accuracy)')
# 核心配置:设置Y轴主刻度仅保留1位小数
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
plt.legend()
plt.show()
  • 相关说明:
    • 如果你不需要修改坐标轴标签为中文,保留原英文标签即可
    • 若需要调整X轴的小数显示格式,把代码中的yaxis替换为xaxis即可
    • Loss曲线的调整逻辑完全一致,在Loss的绘图代码中添加同样的Y轴格式化配置即可生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 16:06:03