Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2840392

Browse files
committed
Merged revisions 8679 via svnmerge from
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v1_0_maint ........ r8679 | efiring | 2010-09-04 14:38:45 -1000 (Sat, 04 Sep 2010) | 2 lines [3024007] Fix ancient bug in hist inherited from numpy, fixed in numpy 1.5 ........ svn path=/trunk/matplotlib/; revision=8680
1 parent cbf3719 commit 2840392

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/matplotlib/axes.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7466,6 +7466,12 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
74667466
pdf, bins, patches = ax.hist(...)
74677467
print np.sum(pdf * np.diff(bins))
74687468
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+
74697475
*weights*
74707476
An array of weights, of the same shape as *x*. Each value in
74717477
*x* only contributes its associated weight towards the bin
@@ -7647,7 +7653,10 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
76477653
xmax = max(xmax, xi.max())
76487654
range = (xmin, xmax)
76497655

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)
76517660
if np.__version__ < "1.3": # version 1.1 and 1.2
76527661
hist_kwargs['new'] = True
76537662

@@ -7656,7 +7665,20 @@ def hist(x, bins=10, range=None, normed=False, weights=None,
76567665
# this will automatically overwrite bins,
76577666
# so that each histogram uses the same bins
76587667
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()
76597671
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+
76607682

76617683
if cumulative:
76627684
slc = slice(None)

0 commit comments

Comments
 (0)