@@ -7466,6 +7466,12 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
7466
7466
pdf, bins, patches = ax.hist(...)
7467
7467
print np.sum(pdf * np.diff(bins))
7468
7468
7469
+ .. Note:: Until numpy release 1.5, the underlying numpy
7470
+ histogram function was incorrect with *normed*=*True*
7471
+ if bin sizes were unequal. MPL inherited that
7472
+ error. It is now corrected within MPL when using
7473
+ earlier numpy versions
7474
+
7469
7475
*weights*
7470
7476
An array of weights, of the same shape as *x*. Each value in
7471
7477
*x* only contributes its associated weight towards the bin
@@ -7647,7 +7653,10 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
7647
7653
xmax = max (xmax , xi .max ())
7648
7654
range = (xmin , xmax )
7649
7655
7650
- hist_kwargs = dict (range = range , normed = bool (normed ))
7656
+ #hist_kwargs = dict(range=range, normed=bool(normed))
7657
+ # We will handle the normed kwarg within mpl until we
7658
+ # get to the point of requiring numpy >= 1.5.
7659
+ hist_kwargs = dict (range = range )
7651
7660
if np .__version__ < "1.3" : # version 1.1 and 1.2
7652
7661
hist_kwargs ['new' ] = True
7653
7662
@@ -7656,7 +7665,20 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
7656
7665
# this will automatically overwrite bins,
7657
7666
# so that each histogram uses the same bins
7658
7667
m , bins = np .histogram (x [i ], bins , weights = w [i ], ** hist_kwargs )
7668
+ if normed :
7669
+ db = np .diff (bins )
7670
+ m = (m .astype (float ) / db ) / m .sum ()
7659
7671
n .append (m )
7672
+ if normed and db .std () > 0.01 * db .mean ():
7673
+ warnings .warn ("""
7674
+ This release fixes a normalization bug in the NumPy histogram
7675
+ function prior to version 1.5, occuring with non-uniform
7676
+ bin widths. The returned and plotted value is now a density:
7677
+ n / (N * bin width),
7678
+ where n is the bin count and N the total number of points.
7679
+ """ )
7680
+
7681
+
7660
7682
7661
7683
if cumulative :
7662
7684
slc = slice (None )
0 commit comments