Cumulative histogram has last point at y=0
Asked Answered
I

3

16

I am creating histogram with

pylab.hist(data,weights,histtype='step',normed=False,bins=150,cumulative=True)

getting (there are other plots, which are irrelevant now) the violet line

histogram

Why is the histogram dropping to zero at the end again? Cumulative functions should be in general non-decreasing. Is there a way to work around this, be it bug or feature?

Iq answered 21/5, 2012 at 17:51 Comment(0)
L
0

This is default behaviour. Think of it as an outline of the histogram as a bar chart. As for a quick workaround, not that I am aware of. A solution would be to calculate the histogram on your own: python histogram one-liner

Latashalatashia answered 21/5, 2012 at 18:10 Comment(1)
Disappointing, but thanks. I can compute the histogram (the one-lines won't do, those are floats which are binned to ragularly-spaced ranges), I actually do it already, though I always prefer tested pre-cooked functions.Iq
U
1

In case you don't like OP's nice simple solution, here's an over-complicated one where we construct the plot by hand. Maybe it's useful though if you only have access to the histogram counts and can't use matplotlib's hist function.

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(5000)
counts, bins = np.histogram(data, bins=20)
cdf = np.cumsum(counts)/np.sum(counts)

plt.plot(
    np.vstack((bins, np.roll(bins, -1))).T.flatten()[:-2],
    np.vstack((cdf, cdf)).T.flatten()
)
plt.show()

output

Undry answered 28/4, 2017 at 12:51 Comment(0)
P
1

Solution (hack):

# histtype=step returns a single patch, open polygon
n,bins,patches=pylab.hist(data, weights, histtype='step', cumulative=True)
# just delete the last point
patches[0].set_xy(patches[0].get_xy()[:-1])

This answer was posted as an edit to the question cumulative histogram has last point at y=0 by the OP Zer0 under CC BY-SA 3.0.

Preciosa answered 3/3, 2023 at 15:29 Comment(0)
L
0

This is default behaviour. Think of it as an outline of the histogram as a bar chart. As for a quick workaround, not that I am aware of. A solution would be to calculate the histogram on your own: python histogram one-liner

Latashalatashia answered 21/5, 2012 at 18:10 Comment(1)
Disappointing, but thanks. I can compute the histogram (the one-lines won't do, those are floats which are binned to ragularly-spaced ranges), I actually do it already, though I always prefer tested pre-cooked functions.Iq

© 2022 - 2024 — McMap. All rights reserved.