Tricks to get reverse-order cumulative histogram in matplotlib
Asked Answered
A

1

12

I am wondering if there is a (better) trick to reverse a cumulative histogram in matplotlib.

Let's say I have some scores in the range of 0.0 to 1.0 where 1.0 is the best score. Now, I am interested to plot how many samples are above a certain score threshold.

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)

enter image description here

By default, matplotlib would plot the cumulative histogram like with 'number of samples <= score'

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

enter image description here

What I acutally want is the cumulative histogram not show 'number of samples <= score' but 'number of samples >= score'

I could do it like this, but then how would I get rid of the "minus" sign on the x-axis?

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

enter image description here

Any better ideas?

Alidis answered 5/3, 2015 at 23:30 Comment(0)
T
23

Pretty sure you can just use cumulative=-1 in your function call:

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

From the matplotlib hist() docs:

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

Take a look at the third example image here; I think it does what you want.

Tenure answered 5/3, 2015 at 23:45 Comment(1)
Thanks, that's exactly what I wanted.Alidis

© 2022 - 2024 — McMap. All rights reserved.