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

Skip to content

Commit 2e1a96d

Browse files
committed
Re-write stacked step histogram
1 parent 9b4e77d commit 2e1a96d

15 files changed

+1953
-21
lines changed

lib/matplotlib/axes.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8108,10 +8108,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
81088108
hist_kwargs['new'] = True
81098109

81108110
n = []
8111-
mlast = None
8112-
# reversed order is necessary so when stacking histogram, first dataset is on top
8113-
# if histogram isn't stacked, this doesn't make any difference
8114-
for i in reversed(xrange(nx)):
8111+
mlast = bottom
8112+
for i in xrange(nx):
81158113
# this will automatically overwrite bins,
81168114
# so that each histogram uses the same bins
81178115
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
@@ -8137,8 +8135,6 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
81378135
else:
81388136
n = [m[slc].cumsum()[slc] for m in n]
81398137

8140-
n.reverse() # put them back in the right order
8141-
81428138
patches = []
81438139

81448140
if histtype.startswith('bar'):
@@ -8175,17 +8171,23 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
81758171
_barfunc = self.bar
81768172

81778173
for m, c in zip(n, color):
8178-
patch = _barfunc(bins[:-1]+boffset, m, width,
8174+
patch = _barfunc(bins[:-1]+boffset, m, width, bottom,
81798175
align='center', log=log,
81808176
color=c, bottom=bottom)
81818177
patches.append(patch)
8178+
if stacked:
8179+
if bottom is None:
8180+
bottom = 0.0
8181+
bottom += m
81828182
boffset += dw
81838183

81848184
elif histtype.startswith('step'):
8185-
x = np.zeros( 2*len(bins), np.float )
8186-
y = np.zeros( 2*len(bins), np.float )
8185+
# these define the perimeter of the polygon
8186+
x = np.zeros( 4*len(bins)-3, np.float )
8187+
y = np.zeros( 4*len(bins)-3, np.float )
81878188

8188-
x[0::2], x[1::2] = bins, bins
8189+
x[0:2*len(bins)-1:2], x[1:2*len(bins)-1:2] = bins, bins[:-1]
8190+
x[2*len(bins)-1:] = x[1:2*len(bins)-1][::-1]
81898191

81908192
if log:
81918193
if orientation == 'horizontal':
@@ -8219,19 +8221,37 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
82198221
# overriding this
82208222
fill = (histtype == 'stepfilled')
82218223

8222-
for m, c in zip(n, color):
8223-
y[1:-1:2], y[2::2] = m, m
8224+
xvals, yvals = [], []
8225+
for m in n:
8226+
# starting point for drawing polygon
8227+
y[0] = y[-1]
8228+
# top of the previous polygon becomes the bottom
8229+
y[2*len(bins)-1:] = y[1:2*len(bins)-1][::-1]
8230+
# set the top of this polygon
8231+
y[1:2*len(bins)-1:2], y[2:2*len(bins):2] = m, m
82248232
if log:
82258233
y[y<minimum]=minimum
82268234
if orientation == 'horizontal':
82278235
x,y = y,x
82288236

8237+
xvals.append(x.copy())
8238+
yvals.append(y.copy())
8239+
8240+
# add patches in reverse order so that when stacking,
8241+
# items lower in the stack are plottted on top of
8242+
# items higher in the stack
8243+
for x, y, c in reversed(zip(xvals, yvals, color)):
82298244
if fill:
82308245
patches.append( self.fill(x, y,
8231-
closed=False, facecolor=c) )
8246+
closed=False,
8247+
facecolor=c) )
82328248
else:
82338249
patches.append( self.fill(x, y,
8234-
closed=False, edgecolor=c, fill=False) )
8250+
closed=False, edgecolor=c,
8251+
fill=False) )
8252+
8253+
# we return patches, so put it back in the expected order
8254+
patches.reverse()
82358255

82368256
# adopted from adjust_x/ylim part of the bar method
82378257
if orientation == 'horizontal':
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)