From 82eeedaf23a32cc082753ebe71cbf35f78ff7684 Mon Sep 17 00:00:00 2001 From: Nic Eggert Date: Sat, 26 Jan 2013 09:56:35 -0500 Subject: [PATCH 1/2] Overhauling stacked histograms --- lib/matplotlib/axes.py | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 35e1e8f5abe4..c02578162cdb 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8076,10 +8076,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, n = [] mlast = bottom - # reversed order is necessary so when stacking histogram, first - # dataset is on top if histogram isn't stacked, this doesn't make any - # difference - for i in reversed(xrange(nx)): + for i in xrange(nx): # 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) @@ -8103,8 +8100,6 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, else: n = [m[slc].cumsum()[slc] for m in n] - n.reverse() # put them back in the right order - patches = [] if histtype.startswith('bar'): @@ -8145,7 +8140,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, patch = _barfunc(bins[:-1] + boffset, m, width, align='center', log=log, color=c) - patches.append(patch) + # need to turn this into a collection so that it's consistent + # with the step histogram + patches.append(mcoll.PatchCollection(patch)) boffset += dw elif histtype.startswith('step'): @@ -8172,7 +8169,10 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, # overriding this fill = (histtype == 'stepfilled') + y_bottom = y[:] for m, c in zip(n, color): + if stacked: + y_bottom = y[:] y[1:-1:2], y[2::2] = m, m if log: y[y < minimum] = minimum @@ -8180,11 +8180,11 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, x, y = y, x if fill: - patches.append(self.fill(x, y, - closed=False, facecolor=c)) + patches.append(self.fill_between(x, y, y_bottom, + closed=False, facecolors=c)) else: - patches.append(self.fill(x, y, - closed=False, edgecolor=c, fill=False)) + patches.append(self.fill_between(x, y, y_bottom, + closed=False, edgecolors=c, fill=False)) # adopted from adjust_x/ylim part of the bar method if orientation == 'horizontal': @@ -8218,17 +8218,11 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, labels += [None] * (nx - len(labels)) for (patch, lbl) in zip(patches, labels): - if patch: - p = patch[0] - p.update(kwargs) - if lbl is not None: - p.set_label(lbl) - - p.set_snap(False) + patch.update(kwargs) + if lbl is not None: + patch.set_label(lbl) - for p in patch[1:]: - p.update(kwargs) - p.set_label('_nolegend_') + patch.set_snap(False) if binsgiven: if orientation == 'vertical': From 33b6daa8bfde80870ea7f3fd7a0f49a4d81ae547 Mon Sep 17 00:00:00 2001 From: Nic Eggert Date: Sat, 26 Jan 2013 10:02:04 -0500 Subject: [PATCH 2/2] Fix some whitespace --- lib/matplotlib/axes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index c02578162cdb..1937de66f36e 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -4450,7 +4450,7 @@ def legend(self, *args, **kwargs): *framealpha*: [*None* | float] If not None, alpha channel for legend frame. Default *None*. - + *ncol* : integer number of columns. default is 1 @@ -8140,8 +8140,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, patch = _barfunc(bins[:-1] + boffset, m, width, align='center', log=log, color=c) - # need to turn this into a collection so that it's consistent - # with the step histogram + # need to turn this into a collection so that it's consistent + # with the step histogram patches.append(mcoll.PatchCollection(patch)) boffset += dw @@ -8169,10 +8169,10 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, # overriding this fill = (histtype == 'stepfilled') - y_bottom = y[:] + y_bottom = y[:] for m, c in zip(n, color): - if stacked: - y_bottom = y[:] + if stacked: + y_bottom = y[:] y[1:-1:2], y[2::2] = m, m if log: y[y < minimum] = minimum