在 matplotlib 中获得倒序累积直方图的技巧

Tricks to get reverse-order cumulative histogram in matplotlib

我想知道是否有(更好的)技巧来反转 matplotlib 中的累积直方图。

假设我有一些分数在 0.0 到 1.0 之间,其中 1.0 是最好的分数。现在,我有兴趣绘制有多少样本高于某个分数阈值。

import numpy as np
import matplotlib.pyplot as plt

d = np.random.normal(size=1000)
d = (d - d.min()) / (d.max() - d.min())

plt.hist(d, 50, histtype="stepfilled", alpha=.7)

默认情况下,matplotlib 会像 'number of samples <= score'

一样绘制累积直方图
plt.hist(d, 50, histtype="stepfilled", alpha=.7, cumulative=True)

我真正想要的是累积直方图不显示 'number of samples <= score' 但 'number of samples >= score'

我可以这样做,但是我如何去掉 x 轴上的 "minus" 符号?

plt.hist(d-1, 50, histtype="stepfilled", alpha=.7, cumulative=True)

有更好的主意吗?

很确定您可以在函数调用中使用 cumulative=-1

plt.hist(d, 50, histtype="stepfilled", alpha=.7, cumulative=-1)

来自 matplotlib hist() 文档:

If cumulative evaluates to less than 0 (e.g., -1), the direction of accumulation is reversed.

看看第三个示例图像here;我认为它可以满足您的需求。