8000 Merge pull request #1833 from neggert/normedstacked_fix · matplotlib/matplotlib@cda4f0d · GitHub
[go: up one dir, main page]

Skip to content

Commit cda4f0d

Browse files
committed
Merge pull request #1833 from neggert/normedstacked_fix
Change hist behavior when normed and stacked to something more sensible
2 parents d63b854 + 4791c9a commit cda4f0d

File tree

6 files changed

+708
-3
lines changed

6 files changed

+708
-3
lines changed

CHANGELOG

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2013-05-08 Changed behavior of hist when given stacked=True and normed=True.
2+
Histograms are now stacked first, then the sum is normalized.
3+
Previously, each histogram was normalized, then they were stacked.
4+
15
2013-04-25 Changed all instances of:
26

37
from matplotlib import MatplotlibDeprecationWarning as mplDeprecation
@@ -13,7 +17,7 @@
1317
margins on auto-scaleing. - TAC
1418

1519
2013-03-19 Added support for passing `linestyle` kwarg to `step` so all `plot`
16-
kwargs are passed to the underlying `plot` call. -TAC
20+
kwargs are passed to the underlying `plot` call. -TAC
1721

1822
2013-02-25 Added classes CubicTriInterpolator, UniformTriRefiner, TriAnalyzer
1923
to matplotlib.tri module. - GBy

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+
7849 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:
Binary file not shown.
Loading

0 commit comments

Comments
 (0)
0