11

我想知道是否有(更好的)技巧来反转 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 将绘制累积直方图,如“样本数 <= 分数”

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

在此处输入图像描述

我真正想要的是累积直方图不显示“样本数 <= 分数”而是“样本数 >= 分数”

我可以这样做,但是我将如何摆脱 x 轴上的“减号”符号?

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

在此处输入图像描述

有更好的想法吗?

4

1 回答 1

19

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

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

来自 matplotlib hist() 文档:

如果累积计算结果小于 0(例如,-1),则累积的方向相反。

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

于 2015-03-05T23:45:27.657 回答