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

Skip to content

Commit d0eb9ca

Browse files
authored
Merge pull request #9121 from dstansby/hist-numpy-doc
Remove old normalising code from plt.hist
2 parents 11dcf76 + 2dde48a commit d0eb9ca

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
@@ -6103,13 +6103,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
61036103
--------
61046104
hist2d : 2D histograms
61056105
6106-
Notes
6107-
-----
6108-
Until numpy release 1.5, the underlying numpy histogram function was
6109-
incorrect with ``normed=True`` if bin sizes were unequal. MPL
6110-
inherited that error. It is now corrected within MPL when using
6111-
earlier numpy versions.
6112-
61136106
"""
61146107
# Avoid shadowing the builtin.
61156108
bin_range = range
@@ -6202,38 +6195,37 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62026195
else:
62036196
hist_kwargs = dict(range=bin_range)
62046197

6205-
n = []
6198+
# List to store all the top coordinates of the histograms
6199+
tops = []
62066200
mlast = None
6201+
# Loop through datasets
62076202
for i in xrange(nx):
62086203
# this will automatically overwrite bins,
62096204
# so that each histogram uses the same bins
62106205
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
62116206
m = m.astype(float) # causes problems later if it's an int
62126207
if mlast is None:
62136208
mlast = np.zeros(len(bins)-1, m.dtype)
6214-
if density and not stacked:
6215-
db = np.diff(bins)
6216-
m = (m.astype(float) / db) / m.sum()
62176209
if stacked:
6218-
if mlast is None:
6219-
mlast = np.zeros(len(bins)-1, m.dtype)
62206210
m += mlast
62216211
mlast[:] = m
6222-
n.append(m)
6212+
tops.append(m)
62236213

6214+
# If a stacked density plot, normalize so the area of all the stacked
6215+
# histograms together is 1
62246216
if stacked and density:
62256217
db = np.diff(bins)
6226-
for m in n:
6227-
m[:] = (m.astype(float) / db) / n[-1].sum()
6218+
for m in tops:
6219+
m[:] = (m.astype(float) / db) / tops[-1].sum()
62286220
if cumulative:
62296221
slc = slice(None)
62306222
if cbook.is_numlike(cumulative) and cumulative < 0:
62316223
slc = slice(None, None, -1)
62326224

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

62386230
patches = []
62396231

@@ -6251,7 +6243,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62516243

62526244
if rwidth is not None:
62536245
dr = np.clip(rwidth, 0, 1)
6254-
elif (len(n) > 1 and
6246+
elif (len(tops) > 1 and
62556247
((not stacked) or rcParams['_internal.classic_mode'])):
62566248
dr = 0.8
62576249
else:
@@ -6277,7 +6269,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62776269
_barfunc = self.bar
62786270
bottom_kwarg = 'bottom'
62796271

6280-
for m, c in zip(n, color):
6272+
for m, c in zip(tops, color):
62816273
if bottom is None:
62826274
bottom = np.zeros(len(m))
62836275
if stacked:
@@ -6321,7 +6313,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63216313
# For data that is normed to form a probability density,
63226314
# set to minimum data value / logbase
63236315
# (gives 1 full tick-label unit for the lowest filled bin)
6324-
ndata = np.array(n)
6316+
ndata = np.array(tops)
63256317
minimum = (np.min(ndata[ndata > 0])) / logbase
63266318
else:
63276319
# For non-normed (density = False) data,
@@ -6344,7 +6336,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63446336
fill = (histtype == 'stepfilled')
63456337

63466338
xvals, yvals = [], []
6347-
for m in n:
6339+
for m in tops:
63486340
if stacked:
63496341
# starting point for drawing polygon
63506342
y[0] = y[1]
@@ -6407,9 +6399,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64076399
p.set_label('_nolegend_')
64086400

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

64146406
@_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None)
64156407
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
@@ -1533,6 +1533,14 @@ def test_hist_step_filled():
15331533
assert all([p.get_facecolor() == p.get_edgecolor() for p in patches])
15341534

15351535

1536+
@image_comparison(baseline_images=['hist_density'], extensions=['png'])
1537+
def test_hist_density():
1538+
np.random.seed(19680801)
1539+
data = np.random.standard_normal(2000)
1540+
fig, ax = plt.subplots()
1541+
ax.hist(data, density=True)
1542+
1543+
15361544
@image_comparison(baseline_images=['hist_step_log_bottom'],
15371545
remove_text=True, extensions=['png'])
15381546
def test_hist_step_log_bottom():

0 commit comments

Comments
 (0)