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?

smci
  • 32,567
  • 20
  • 113
  • 146

1 Answers1

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.

Zachary Cross
  • 2,298
  • 1
  • 15
  • 22