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

Skip to content

Commit f8f6385

Browse files
authored
Merge pull request #9563 from matplotlib/auto-backport-of-pr-9121
Backport PR #9121 on branch v2.1.x
2 parents 187dd92 + 6a36fa1 commit f8f6385

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
@@ -6110,13 +6110,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
61106110
--------
61116111
hist2d : 2D histograms
61126112
6113-
Notes
6114-
-----
6115-
Until numpy release 1.5, the underlying numpy histogram function was
6116-
incorrect with ``normed=True`` if bin sizes were unequal. MPL
6117-
inherited that error. It is now corrected within MPL when using
6118-
earlier numpy versions.
6119-
61206113
"""
61216114
# Avoid shadowing the builtin.
61226115
bin_range = range
@@ -6209,38 +6202,37 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62096202
else:
62106203
hist_kwargs = dict(range=bin_range)
62116204

6212-
n = []
6205+
# List to store all the top coordinates of the histograms
6206+
tops = []
62136207
mlast = None
6208+
# Loop through datasets
62146209
for i in xrange(nx):
62156210
# this will automatically overwrite bins,
62166211
# so that each histogram uses the same bins
62176212
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
62186213
m = m.astype(float) # causes problems later if it's an int
62196214
if mlast is None:
62206215
mlast = np.zeros(len(bins)-1, m.dtype)
6221-
if density and not stacked:
6222-
db = np.diff(bins)
6223-
m = (m.astype(float) / db) / m.sum()
62246216
if stacked:
6225-
if mlast is None:
6226-
mlast = np.zeros(len(bins)-1, m.dtype)
62276217
m += mlast
62286218
mlast[:] = m
6229-
n.append(m)
6219+
tops.append(m)
62306220

6221+
# If a stacked density plot, normalize so the area of all the stacked
6222+
# histograms together is 1
62316223
if stacked and density:
62326224
db = np.diff(bins)
6233-
for m in n:
6234-
m[:] = (m.astype(float) / db) / n[-1].sum()
6225+
for m in tops:
6226+
m[:] = (m.astype(float) / db) / tops[-1].sum()
62356227
if cumulative:
62366228
slc = slice(None)
62376229
if cbook.is_numlike(cumulative) and cumulative < 0:
62386230
slc = slice(None, None, -1)
62396231

62406232
if density:
6241-
n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n]
6233+
tops = [(m * np.diff(bins))[slc].cumsum()[slc] for m in tops]
62426234
else:
6243-
n = [m[slc].cumsum()[slc] for m in n]
6235+
tops = [m[slc].cumsum()[slc] for m in tops]
62446236

62456237
patches = []
62466238

@@ -6258,7 +6250,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62586250

62596251
if rwidth is not None:
62606252
dr = np.clip(rwidth, 0, 1)
6261-
elif (len(n) > 1 and
6253+
elif (len(tops) > 1 and
62626254
((not stacked) or rcParams['_internal.classic_mode'])):
62636255
dr = 0.8
62646256
else:
@@ -6284,7 +6276,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62846276
_barfunc = self.bar
62856277
bottom_kwarg = 'bottom'
62866278

6287-
for m, c in zip(n, color):
6279+
for m, c in zip(tops, color):
62886280
if bottom is None:
62896281
bottom = np.zeros(len(m))
62906282
if stacked:
@@ -6328,7 +6320,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63286320
# For data that is normed to form a probability density,
63296321
# set to minimum data value / logbase
63306322
# (gives 1 full tick-label unit for the lowest filled bin)
6331-
ndata = np.array(n)
6323+
ndata = np.array(tops)
63326324
minimum = (np.min(ndata[ndata > 0])) / logbase
63336325
else:
63346326
# For non-normed (density = False) data,
@@ -6351,7 +6343,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63516343
fill = (histtype == 'stepfilled')
63526344

63536345
xvals, yvals = [], []
6354-
for m in n:
6346+
for m in tops:
63556347
if stacked:
63566348
# starting point for drawing polygon
63576349
y[0] = y[1]
@@ -6414,9 +6406,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64146406
p.set_label('_nolegend_')
64156407

64166408
if nx == 1:
6417-
return n[0], bins, cbook.silent_list('Patch', patches[0])
6409+
return tops[0], bins, cbook.silent_list('Patch', patches[0])
64186410
else:
6419-
return n, bins, cbook.silent_list('Lists of Patches', patches)
6411+
return tops, bins, cbook.silent_list('Lists of Patches', patches)
64206412

64216413
@_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None)
64226414
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)