Skip to content
Advertisement

Why Matplotlib.hist taking different list?

I’m trying to plot the following

list_a_seq_of_p = [0,  1,  1,  1.5, 2.5, 3,  3,  3.5, 4.5, 5 ]
plt.plot(list_a_seq_of_p)
pyplot.hist(
    list_a_seq_of_p,
    range(11),
    histtype="step",
    cumulative=True,
    color=("r"),
    label=("A"),
)

And getting enter image description here

This one actually draws a different array([ 1., 4., 5., 8., 9., 10., 10., 10., 10., 10.], which is created run time automatically.

Advertisement

Answer

I assume you mean the red histogram appears to be wrong? It’s not. You’ve got cumulative=True so it adds each value to make a “running total”.

If you wanted both to line up, provide the same y-axis for both and set cumulative to False (or remove it since False is the default).

import matplotlib.pyplot as plt

list_a_seq_of_p = [0,  1,  1,  1.5, 2.5, 3,  3,  3.5, 4.5, 5 ]
plt.plot(list_a_seq_of_p, range(len(list_a_seq_of_p)))
plt.hist(
    list_a_seq_of_p,
    len(list_a_seq_of_p),
    histtype="step",
    cumulative=False,
    color=("r"),
    label=("A"),
)

That gives:
enter image description here

Btw, the 2nd parameter to plt.hist() is the bins. So which bins are you expecting? len(list_a_seq_of_p) above is not a sensible value for bins.

Also, be consistent with your import names. First you use plt.plot() and next line is pyplot.hist().

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement