Skip to content

Commit 7ba1743

Browse files
committed
Add stacked kwarg to hist. This allows stacked step and stepfilled hists.
1 parent 086c0a6 commit 7ba1743

File tree

6 files changed

+608
-11
lines changed

6 files changed

+608
-11
lines changed

examples/pylab_examples/histogram_demo_extended.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,19 @@
8282
#
8383
P.figure()
8484

85-
n, bins, patches = P.hist(x, 10, normed=1, histtype='barstacked')
85+
n, bins, patches = P.hist(x, 10, normed=1, histtype='bar', stacked=True)
86+
87+
P.show()
88+
89+
#
90+
# we can also stack using the step histtype
91+
#
92+
93+
P.figure()
94+
95+
n, bins, patches = P.hist(x, 10, histtype='step', stacked=True, fill=True)
96+
97+
P.show()
8698

8799
#
88100
# finally: make a multiple-histogram of data-sets with different length

lib/matplotlib/axes.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7728,7 +7728,7 @@ def get_shared_y_axes(self):
77287728
def hist(self, x, bins=10, range=None, normed=False, weights=None,
77297729
cumulative=False, bottom=None, histtype='bar', align='mid',
77307730
orientation='vertical', rwidth=None, log=False,
7731-
color=None, label=None,
7731+
color=None, label=None, stacked=False,
77327732
**kwargs):
77337733
"""
77347734
Plot a histogram.
@@ -7738,7 +7738,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
77387738
hist(x, bins=10, range=None, normed=False, weights=None,
77397739
cumulative=False, bottom=None, histtype='bar', align='mid',
77407740
orientation='vertical', rwidth=None, log=False,
7741-
color=None, label=None,
7741+
color=None, label=None, stacked=False,
77427742
**kwargs)
77437743
77447744
Compute and draw the histogram of *x*. The return value is a
@@ -7862,6 +7862,11 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
78627862
ax.hist(12+3*np.random.randn(1000), label='women', alpha=0.5)
78637863
ax.legend()
78647864
7865+
*stacked*:
7866+
If *True*, multiple data are stacked on top of each other
7867+
If *False* multiple data are aranged side by side if
7868+
histtype is 'bar' or on top of each other if histtype is 'step'
7869+
78657870
.
78667871
78677872
kwargs are used to update the properties of the
@@ -7901,6 +7906,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
79017906
'hist now uses the rwidth to give relative width '
79027907
'and not absolute width')
79037908

7909+
if histtype == 'barstacked' and not stacked:
7910+
stacked=True
7911+
79047912
# Massage 'x' for processing.
79057913
# NOTE: Be sure any changes here is also done below to 'weights'
79067914
if isinstance(x, np.ndarray) or not iterable(x[0]):
@@ -7919,6 +7927,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
79197927
# multiple hist with data of different length
79207928
x = [np.asarray(xi) for xi in x]
79217929

7930+
x = x[::-1] # reverse datasets for caculating stacked hist
7931+
79227932
nx = len(x) # number of datasets
79237933

79247934
if color is None:
@@ -7942,6 +7952,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
79427952
else:
79437953
w = [np.asarray(wi) for wi in weights]
79447954

7955+
w = w[::-1] # reverse weights to match datasets
7956+
79457957
if len(w) != nx:
79467958
raise ValueError('weights should have the same shape as x')
79477959
for i in xrange(nx):
@@ -7986,13 +7998,19 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
79867998
hist_kwargs['new'] = True
79877999

79888000
n = []
8001+
mlast = bottom
79898002
for i in xrange(nx):
79908003
# this will automatically overwrite bins,
79918004
# so that each histogram uses the same bins
79928005
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
8006+
if mlast == None :
8007+
mlast = np.zeros(len(bins)-1, np.int)
79938008
if normed:
79948009
db = np.diff(bins)
79958010
m = (m.astype(float) / db) / m.sum()
8011+
if stacked :
8012+
m += mlast
8013+
mlast[:] = m
79968014
n.append(m)
79978015

79988016
if cumulative:
@@ -8005,6 +8023,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
80058023
else:
80068024
n = [m[slc].cumsum()[slc] for m in n]
80078025

8026+
if stacked :
8027+
n.reverse() # put them back in the right order
8028+
80088029
patches = []
80098030

80108031
if histtype.startswith('bar'):
@@ -8017,7 +8038,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
80178038
else:
80188039
dr = 1.0
80198040

8020-
if histtype=='bar':
8041+
if histtype=='bar' and not stacked:
80218042
width = dr*totwidth/nx
80228043
dw = width
80238044

@@ -8026,10 +8047,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
80268047
else:
80278048
boffset = 0.0
80288049
stacked = False
8029-
elif histtype=='barstacked':
8050+
elif histtype=='barstacked' or stacked:
80308051
width = dr*totwidth
80318052
boffset, dw = 0.0, 0.0
8032-
stacked = True
80338053

80348054
if align == 'mid' or align == 'edge':
80358055
boffset += 0.5*totwidth
@@ -8042,14 +8062,10 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
80428062
_barfunc = self.bar
80438063

80448064
for m, c in zip(n, color):
8045-
patch = _barfunc(bins[:-1]+boffset, m, width, bottom,
8065+
patch = _barfunc(bins[:-1]+boffset, m, width,
80468066
align='center', log=log,
80478067
color=c)
80488068
patches.append(patch)
8049-
if stacked:
8050-
if bottom is None:
8051-
bottom = 0.0
8052-
bottom += m
80538069
boffset += dw
80548070

80558071
elif histtype.startswith('step'):
@@ -8072,6 +8088,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
80728088
else: # orientation == 'vertical'
80738089
self.set_yscale('log')
80748090

8091+
# If fill kwarg is set, it will be passed to the patch collection,
8092+
# overriding this
80758093
fill = (histtype == 'stepfilled')
80768094

80778095
for m, c in zip(n, color):
5.38 KB
Binary file not shown.
5.85 KB
Loading

0 commit comments

Comments
 (0)