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

如何移除温度直方图无数据空白区域并识别异常数据?

解决温度数据直方图空白区域与数据保留问题

针对你的需求,这里提供三个实用方案,既能移除无数据的空白区间,又能完整保留所有数据的可视化与异常值统计:

方案1:聚焦正常区间+标注异常值

直接将X轴范围限定在覆盖正常温度的区间(比如±200),同时在图上标注异常值的数量和占比,既去掉空白区域,又能清晰掌握异常情况。

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# 替换为你的温度数据数组
temp_data = np.concatenate([np.random.normal(0, 50, 990), np.random.choice([-8000, 9000], 10)])

# 统计异常值
outliers = temp_data[(temp_data < -150) | (temp_data > 150)]
outlier_ratio = len(outliers)/len(temp_data)*100

# 绘制直方图并限定X轴
sns.histplot(temp_data, bins=30)
plt.xlim(-200, 200)
plt.yscale('log')
# 添加异常值统计标注
plt.text(0, plt.ylim()[1]*0.9, f'异常值数量: {len(outliers)}\n占比: {outlier_ratio:.2f}%', ha='center')
plt.title('温度数据直方图(聚焦正常范围)')
plt.xlabel('温度')
plt.ylabel('数量(对数刻度)')
plt.show()

方案2:分段坐标轴展示

用两个子图分别展示正常温度区间和异常值区间,彻底避免空白区域,同时直观对比两者的分布与占比。

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5), gridspec_kw={'width_ratios': [3, 1]})

# 左子图:正常温度区间
sns.histplot(temp_data[(temp_data >= -200) & (temp_data <= 200)], bins=30, ax=ax1)
ax1.set_xlim(-200, 200)
ax1.set_yscale('log')
ax1.set_title('正常温度区间分布')
ax1.set_xlabel('温度')
ax1.set_ylabel('数量(对数刻度)')

# 右子图:异常值区间
sns.histplot(temp_data[(temp_data < -200) | (temp_data > 200)], bins=2, ax=ax2)
ax2.set_xlim(-9000, 10000)
ax2.set_title('异常值分布')
ax2.set_xlabel('温度')

plt.tight_layout()
plt.show()

方案3:自定义直方图Bins

手动设置包含数据的区间作为bins,跳过无数据的空白段,让直方图只绘制有数据的部分,同时保留所有数据点。

import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

temp_data = np.concatenate([np.random.normal(0, 50, 990), np.random.choice([-8000, 9000], 10)])

# 自定义bins:包含异常值端点+正常区间的细分间隔
custom_bins = [-8000] + list(np.arange(-150, 151, 20)) + [9000]
sns.histplot(temp_data, bins=custom_bins)
plt.yscale('log')
plt.title('温度数据直方图(自定义Bins)')
plt.xlabel('温度')
plt.ylabel('数量(对数刻度)')
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 02:07:06