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

使用matplotlib的fill_between绘制填充区域时y轴偏移问题求助

问题解决:填充区域与折线偏移的修复

你的问题出在fill_between的x轴参数和plt.plot使用的x轴数据不匹配:

  • 折线图用df1['band']作为x轴坐标
  • 填充区域却用range(len(df1["d_bas"]))生成的连续整数序列

如果df1['band']的值不是从0开始的连续整数(或者和range序列不完全一致),再加上y轴是对数刻度,就会导致填充区域和折线出现明显偏移。

修复步骤

把两个fill_between的第一个参数替换成df1['band'],确保和折线的x轴完全对齐:

d_bas_q1 = df1["d_bas"] + df1['d_bas_q1']
d_bas_q3 = df1["d_bas"] + df1['d_bas_q3']
n_bas_q1 = df1["n_bas"] + df1['n_bas_q1']
n_bas_q3 = df1["n_bas"] + df1['n_bas_q3']


plt.figure(figsize=((10,8))) 

plt.plot(df1['band'], df1["p_bas_cond"], color='k', alpha=0.1, linewidth=2)
plt.plot(df1['band'], df1["d_bas"], color='wheat',alpha=1, linewidth=2, label='m')

# 替换x轴参数为df1['band']
plt.fill_between(df1['band'], d_bas_q1, d_bas_q3, alpha=0.4, facecolor = 'wheat')

plt.plot(df1['band'], df1["n_bas"], color='cornflowerblue', alpha=0.7, linewidth=2, label='w')

# 替换x轴参数为df1['band']
plt.fill_between(df1['band'], n_bas_q1, n_bas_q3, alpha=0.4, facecolor = 'cornflowerblue')

plt.legend(loc="upper right") 

plt.yscale('log') 


plt.xlim([0, 10]) 
plt.ylim([plot_ymin1, plot_ymax1]) 
plt.xticks(range(0, 11, 1)) 
plt.minorticks_off()

plt.xlabel("x")

plt.ylabel('y') 

plt.show()

额外提醒:通常误差带是均值加减分位数/标准差,你当前用d_bas + d_bas_q1作为边界,如果d_bas_q1是中位数到Q1的差值,可能需要调整为d_bas - d_bas_q1作为下界、d_bas + d_bas_q3作为上界,具体根据你的数据定义调整即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 21:40:57