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

Python中绘制反向累积直方图:调整X轴从100到20递减的方法

解决方案

方法1:直接绘制反向累积直方图(无需修改原始数据)

利用Matplotlib的hist函数的cumulative=-1参数,可直接实现“每个区间包含该区间及以上数值”的累积逻辑,再通过反转X轴达成从100到20的递减显示,完全无需改动原始数据。

代码示例:

import matplotlib.pyplot as plt
import pandas as pd

# 构建示例数据
data = {'pident': [100, 100, 51.515, 55.405, 20, 91.667, 86.207, 58.621, 77.778]}
df = pd.DataFrame(data)

# 绘制反向累积直方图
plt.hist(df['pident'], bins=5, cumulative=-1, edgecolor='black')

# 反转X轴,实现数值从100到20递减
plt.gca().invert_xaxis()

# 设置图表标签与标题
plt.xlabel('匹配百分比(pident)')
plt.ylabel('累积样本数量')
plt.title('匹配百分比反向累积直方图')

plt.show()

说明:cumulative=-1会让直方图从最高值开始累积,统计每个区间及以上的样本总数;invert_xaxis()直接反转X轴方向,完美契合需求。

方法2:修正你原方案的X轴标签

如果仍想保留abs_pident = np.abs(100 - df['pident'])的计算方式,可手动替换X轴刻度标签,将转换后的数值还原为原始匹配百分比:

代码示例:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 构建示例数据
data = {'pident': [100, 100, 51.515, 55.405, 20, 91.667, 86.207, 58.621, 77.778]}
df = pd.DataFrame(data)
df['abs_pident'] = np.abs(100 - df['pident'])

# 绘制累积直方图(基于转换后的数据)
n, bins, patches = plt.hist(df['abs_pident'], bins=5, cumulative=True, edgecolor='black')

# 将X轴刻度替换为原始匹配百分比(100 - 转换后数值)
plt.xticks(bins, [round(100 - b, 1) for b in bins])

# 设置图表标签与标题
plt.xlabel('匹配百分比(pident)')
plt.ylabel('累积样本数量')
plt.title('匹配百分比反向累积直方图')

plt.show()

说明:通过100 - bins将转换后的刻度值还原为原始匹配百分比,确保X轴显示正确的数值范围。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 01:37:12