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

如何用Matplotlib绘制含均值与标准差的月度动物活动距离图表

用Matplotlib绘制带误差棒的月度动物活动距离图表

给定全年12个月的动物平均活动距离列表mean和对应标准差列表StDev,以下是复刻目标样式图表的实现方案:

数据准备

mean = [7.51790364988932, 26.54709179011294, 29.65764360621555, 136.671273129226, 126.13064622076125, 25.366008757314532, 4.663780674841924, 3.7616067897445777, 4.330291751722638, 25.038066596104834, 92.81906072064585, 45.74368656241264]
StDev = [5.927150675222632, 15.59253255407175, 16.312448356657924, 81.31314415973934, 41.580975203922236, 11.480435976180836, 2.9712310351842786, 1.9313303794907384, 2.063154699821021, 30.746712593757756, 44.5391819737211, 11.590817409187078]
months = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]

绘图代码

import matplotlib.pyplot as plt

# 创建画布与轴对象
fig, ax = plt.subplots(figsize=(10, 6))

# 绘制带误差棒的折线图,匹配目标样式
ax.errorbar(months, mean, yerr=StDev, fmt='-o', color='#1f77b4', 
            ecolor='#ff7f0e', capsize=5, linewidth=2, markersize=8)

# 设置图表标签与标题
ax.set_xlabel("月份", fontsize=12)
ax.set_ylabel("平均活动距离", fontsize=12)
ax.set_title("全年各月动物平均活动距离(带标准差误差棒)", fontsize=14)

# 自动调整布局,避免标签重叠
plt.tight_layout()

# 展示图表
plt.show()

关键参数说明

  • errorbar是核心函数:yerr传入标准差列表实现误差棒;fmt='-o'定义折线为实线+圆形标记;capsize控制误差棒两端短帽长度;ecolor单独指定误差棒颜色。
  • 通过figsize调整画布尺寸,fontsize控制文字显示大小。
  • tight_layout()自动优化元素位置,防止标签被截断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 05:25:52