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

Python中如何去除图表因极值导致的多余空白区域?

解决极值导致图表多余空白的方法

你的图表因数据中存在极值,导致X轴被过度拉伸,出现大量无效空白。可以通过以下几种方式处理:

  • 手动限制X轴显示范围:利用plt.xlim()指定X轴的显示区间,聚焦大部分数据的分布区域。比如用数据的99分位数作为上限,自动适配数据分布:
plt.figure()
sns.distplot(precip)
plt.title('Precipitation')
plt.xlabel('Precipitation (mm)')
plt.xlim(0, precip.quantile(0.99))
plt.show()
  • 过滤极端值数据:先对原始数据做预处理,剔除超出合理范围的极值后再绘图,比如保留99%以内的数据:
# 过滤极值,保留99分位数以内的数据
filtered_precip = precip[precip <= precip.quantile(0.99)]

plt.figure()
sns.distplot(filtered_precip)
plt.title('Precipitation')
plt.xlabel('Precipitation (mm)')
plt.show()
  • 使用对数坐标轴:如果数据分布呈严重偏态,对数变换可以压缩极值的影响,让主体数据的分布更清晰(注意数据不能包含0,否则需先做偏移处理,比如precip + 1):
plt.figure()
sns.distplot(precip)
plt.title('Precipitation')
plt.xlabel('Precipitation (mm)')
plt.xscale('log')
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:19:54