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

Skip to content

Commit 6a36fa1

Browse files
phobsonMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR #9121: Remove old normalising code from plt.hist
1 parent 3f62592 commit 6a36fa1

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6105,13 +6105,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
61056105
--------
61066106
hist2d : 2D histograms
61076107
6108-
Notes
6109-
-----
6110-
Until numpy release 1.5, the underlying numpy histogram function was
6111-
incorrect with ``normed=True`` if bin sizes were unequal. MPL
6112-
inherited that error. It is now corrected within MPL when using
6113-
earlier numpy versions.
6114-
61156108
"""
61166109
# Avoid shadowing the builtin.
61176110
bin_range = range
@@ -6204,38 +6197,37 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62046197
else:
62056198
hist_kwargs = dict(range=bin_range)
62066199

6207-
n = []
6200+
# List to store all the top coordinates of the histograms
6201+
tops = []
62086202
mlast = None
6203+
# Loop through datasets
62096204
for i in xrange(nx):
62106205
# this will automatically overwrite bins,
62116206
# so that each histogram uses the same bins
62126207
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
62136208
m = m.astype(float) # causes problems later if it's an int
62146209
if mlast is None:
62156210
mlast = np.zeros(len(bins)-1, m.dtype)
6216-
if density and not stacked:
6217-
db = np.diff(bins)
6218-
m = (m.astype(float) / db) / m.sum()
62196211
if stacked:
6220-
if mlast is None:
6221-
mlast = np.zeros(len(bins)-1, m.dtype)
62226212
m += mlast
62236213
mlast[:] = m
6224-
n.append(m)
6214+
tops.append(m)
62256215

6216+
# If a stacked density plot, normalize so the area of all the stacked
6217+
# histograms together is 1
62266218
if stacked and density:
62276219
db = np.diff(bins)
6228-
for m in n:
6229-
m[:] = (m.astype(float) / db) / n[-1].sum()
6220+
for m in tops:
6221+
m[:] = (m.astype(float) / db) / tops[-1].sum()
62306222
if cumulative:
62316223
slc = slice(None)
62326224
if cbook.is_numlike(cumulative) and cumulative < 0:
62336225
slc = slice(None, None, -1)
62346226

62356227
if density:
6236-
n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n]
6228+
tops = [(m * np.diff(bins))[slc].cumsum()[slc] for m in tops]
62376229
else:
6238-
n = [m[slc].cumsum()[slc] for m in n]
6230+
tops = [m[slc].cumsum()[slc] for m in tops]
62396231

62406232
patches = []
62416233

@@ -6253,7 +6245,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62536245

62546246
if rwidth is not None:
62556247
dr = np.clip(rwidth, 0, 1)
6256-
elif (len(n) > 1 and
6248+
elif (len(tops) > 1 and
62576249
((not stacked) or rcParams['_internal.classic_mode'])):
62586250
dr = 0.8
62596251
else:
@@ -6279,7 +6271,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62796271
_barfunc = self.bar
62806272
bottom_kwarg = 'bottom'
62816273

6282-
for m, c in zip(n, color):
6274+
for m, c in zip(tops, color):
62836275
if bottom is None:
62846276
bottom = np.zeros(len(m))
62856277
if stacked:
@@ -6323,7 +6315,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63236315
# For data that is normed to form a probability density,
63246316
# set to minimum data value / logbase
63256317
# (gives 1 full tick-label unit for the lowest filled bin)
6326-
ndata = np.array(n)
6318+
ndata = np.array(tops)
63276319
minimum = (np.min(ndata[ndata > 0])) / logbase
63286320
else:
63296321
# For non-normed (density = False) data,
@@ -6346,7 +6338,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63466338
fill = (histtype == 'stepfilled')
63476339

63486340
xvals, yvals = [], []
6349-
for m in n:
6341+
for m in tops:
63506342
if stacked:
63516343
# starting point for drawing polygon
63526344
y[0] = y[1]
@@ -6409,9 +6401,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64096401
p.set_label('_nolegend_')
64106402

64116403
if nx == 1:
6412-
return n[0], bins, cbook.silent_list('Patch', patches[0])
6404+
return tops[0], bins, cbook.silent_list('Patch', patches[0])
64136405
else:
6414-
return n, bins, cbook.silent_list('Lists of Patches', patches)
6406+
return tops, bins, cbook.silent_list('Lists of Patches', patches)
64156407

64166408
@_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None)
64176409
def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,14 @@ def test_hist_step_filled():
14931493
assert all([p.get_facecolor() == p.get_edgecolor() for p in patches])
14941494

14951495

1496+
@image_comparison(baseline_images=['hist_density'], extensions=['png'])
1497+
def test_hist_density():
1498+
np.random.seed(19680801)
1499+
data = np.random.standard_normal(2000)
1500+
fig, ax = plt.subplots()
1501+
ax.hist(data, density=True)
1502+
1503+
14961504
@image_comparison(baseline_images=['hist_step_log_bottom'],
14971505
remove_text=True, extensions=['png'])
14981506
def test_hist_step_log_bottom():

0 commit comments

Comments
 (0)