diff --git a/CHANGELOG b/CHANGELOG index 8f810db398ea..221ea4995147 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +2013-05-08 Changed behavior of hist when given stacked=True and normed=True. + Histograms are now stacked first, then the sum is normalized. + Previously, each histogram was normalized, then they were stacked. + 2013-04-25 Changed all instances of: from matplotlib import MatplotlibDeprecationWarning as mplDeprecation @@ -13,7 +17,7 @@ margins on auto-scaleing. - TAC 2013-03-19 Added support for passing `linestyle` kwarg to `step` so all `plot` - kwargs are passed to the underlying `plot` call. -TAC + kwargs are passed to the underlying `plot` call. -TAC 2013-02-25 Added classes CubicTriInterpolator, UniformTriRefiner, TriAnalyzer to matplotlib.tri module. - GBy diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 850146384085..4ab860a3c1af 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8088,7 +8088,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, If `True`, the first element of the return tuple will be the counts normalized to form a probability density, i.e., ``n/(len(x)`dbin)``, ie the integral of the histogram will sum to - 1. + 1. If *stacked* is also *True*, the sum of the histograms is + normalized to 1. weights : array_like, shape (n, ), optional, default: None An array of weights, of the same shape as `x`. Each value in `x` @@ -8300,9 +8301,10 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, # this will automatically overwrite bins, # so that each histogram uses the same bins m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) + m = m.astype(float) # causes problems later if it's an int if mlast is None: mlast = np.zeros(len(bins)-1, m.dtype) - if normed: + if normed and not stacked: db = np.diff(bins) m = (m.astype(float) / db) / m.sum() if stacked: @@ -8310,6 +8312,10 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, mlast[:] = m n.append(m) + if stacked and normed: + db = np.diff(bins) + for m in n: + m[:] = (m.astype(float) / db) / n[-1].sum() if cumulative: slc = slice(None) if cbook.is_numlike(cumulative) and cumulative < 0: diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.pdf b/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.pdf new file mode 100644 index 000000000000..b5a3595dfa06 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.png b/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.png new file mode 100644 index 000000000000..245321815507 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.svg b/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.svg new file mode 100644 index 000000000000..938419c46db3 --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_axes/hist_stacked_normed.svg @@ -0,0 +1,684 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9f8e547eb4eb..29af0a604409 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1072,6 +1072,17 @@ def test_hist_stacked_step(): ax = fig.add_subplot(111) ax.hist( (d1, d2), histtype="step", stacked=True) + +@image_comparison(baseline_images=['hist_stacked_normed']) +def test_hist_stacked_normed(): + # make some data + d1 = np.linspace(1, 3, 20) + d2 = np.linspace(0, 10, 50) + fig = plt.figure() + ax = fig.add_subplot(111) + ax.hist((d1, d2), stacked=True, normed=True) + + @image_comparison(baseline_images=['hist_stacked_bar']) def test_hist_stacked_bar(): # make some data