X Tutup
Skip to content

pyplot.hist: normalization fails #6357

@khyox

Description

@khyox

Matplotlib 1.5.1 under python 3.5.1.
The provided example for pyplot.hist is not working as expected for different mu and sigma values:

"""
Demo of the histogram (hist) function with a few features.

In addition to the basic histogram, this demo shows a few optional features:

    * Setting the number of data bins
    * The ``normed`` flag, which normalizes bin heights so that the integral of
      the histogram is 1. The resulting histogram is a probability density.
    * Setting the face color of the bars
    * Setting the opacity (alpha value).

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


# example data
mu = 0.5*5  # mean of distribution
sigma = 0.1*5  # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)

num_bins = 50
# the histogram of the data
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Normed histogram of IQ: $\mu=%f$, $\sigma=%f$' % (mu, sigma))

# Tweak spacing to prevent clipping of ylabel
plt.subplots_adjust(left=0.15)
plt.show()

hist_normed

If the parameters mu and sigma are reduced to:

mu = 0.5*2  # mean of distribution
sigma = 0.1*2  # standard deviation of distribution

the plot becomes even more clearly unnormalized:

hist_normed_fail

This fail with normalization also happens for non-gaussian datasets.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup