[Python] matplotlib hist function argument density not working

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    plt.hist's density argument does not work.

    I tried to use the density argument in the plt.hist function to normalize stock returns in my plot, but it didn't work.

    The following code worked fine for me and give me the probability density function which I desired.

    import matplotlib
    import numpy as np
    import matplotlib.pyplot as plt

    np.random.seed(19680801)

    # example data
    mu = 100 # mean of distribution
    sigma = 15 # standard deviation of distribution
    x = mu + sigma * np.random.randn(437)

    num_bins = 50

    plt.hist(x, num_bins, density=1)

    plt.show()


    [​IMG]

    But when I tried it with stock data, it simply didn't work. The result gave the unnormalized data. I didn't find any abnormal data in my data array.

    import numpy as np
    import matplotlib.pyplot as plt
    fig = plt.figure()
    plt.hist(returns, 50,density = True)
    plt.show()
    # "returns" is a np array consisting of 360 days of stock returns


    [​IMG]

    Continue reading...

Compartilhe esta Página