Skip to content

Commit 6917f4f

Browse files
committed
Histograms with stacked=True, normed=True now normalize sum of histograms rather than each histogram individually.
1 parent d63b854 commit 6917f4f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/matplotlib/axes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8088,7 +8088,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
80888088
If `True`, the first element of the return tuple will
80898089
be the counts normalized to form a probability density, i.e.,
80908090
``n/(len(x)`dbin)``, ie the integral of the histogram will sum to
8091-
1.
8091+
1. If *stacked* is also *True*, the sum of the histograms is
8092+
normalized to 1.
80928093
80938094
weights : array_like, shape (n, ), optional, default: None
80948095
An array of weights, of the same shape as `x`. Each value in `x`
@@ -8300,16 +8301,21 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
83008301
# this will automatically overwrite bins,
83018302
# so that each histogram uses the same bins
83028303
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
8304+
m = m.astype(float) # causes problems later if it's an int
83038305
if mlast is None:
83048306
mlast = np.zeros(len(bins)-1, m.dtype)
8305-
if normed:
8307+
if normed and not stacked:
83068308
db = np.diff(bins)
83078309
m = (m.astype(float) / db) / m.sum()
83088310
if stacked:
83098311
m += mlast
83108312
mlast[:] = m
83118313
n.append(m)
83128314

8315+
if stacked and normed:
8316+
db = np.diff(bins)
8317+
for m in n:
8318+
m[:] = (m.astype(float) / db) / n[-1].sum()
83138319
if cumulative:
83148320
slc = slice(None)
83158321
if cbook.is_numlike(cumulative) and cumulative < 0:

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,15 @@ def test_hist_stacked_step():
10721072
ax = fig.add_subplot(111)
10731073
ax.hist( (d1, d2), histtype="step", stacked=True)
10741074

1075+
@image_comparison(baseline_images=['hist_stacked_normed'])
1076+
def test_hist_stacked_normed():
1077+
# make some data
1078+
d1 = np.linspace(1, 3, 20)
1079+
d2 = np.linspace(0, 10, 50)
1080+
fig = plt.figure()
1081+
ax = fig.add_subplot(111)
1082+
ax.hist( (d1, d2), stacked=True, normed=True)
1083+
10751084
@image_comparison(baseline_images=['hist_stacked_bar'])
10761085
def test_hist_stacked_bar():
10771086
# make some data

0 commit comments

Comments
 (0)