@@ -7779,7 +7779,7 @@ def get_shared_y_axes(self):
7779
7779
def hist (self , x , bins = 10 , range = None , normed = False , weights = None ,
7780
7780
cumulative = False , bottom = None , histtype = 'bar' , align = 'mid' ,
7781
7781
orientation = 'vertical' , rwidth = None , log = False ,
7782
- color = None , label = None ,
7782
+ color = None , label = None , stacked = False ,
7783
7783
** kwargs ):
7784
7784
"""
7785
7785
Plot a histogram.
@@ -7789,7 +7789,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
7789
7789
hist(x, bins=10, range=None, normed=False, weights=None,
7790
7790
cumulative=False, bottom=None, histtype='bar', align='mid',
7791
7791
orientation='vertical', rwidth=None, log=False,
7792
- color=None, label=None,
7792
+ color=None, label=None, stacked=False,
7793
7793
**kwargs)
7794
7794
7795
7795
Compute and draw the histogram of *x*. The return value is a
@@ -7913,6 +7913,11 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
7913
7913
ax.hist(12+3*np.random.randn(1000), label='women', alpha=0.5)
7914
7914
ax.legend()
7915
7915
7916
+ *stacked*:
7917
+ If *True*, multiple data are stacked on top of each other
7918
+ If *False* multiple data are aranged side by side if
7919
+ histtype is 'bar' or on top of each other if histtype is 'step'
7920
+
7916
7921
.
7917
7922
7918
7923
kwargs are used to update the properties of the
@@ -7952,6 +7957,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
7952
7957
'hist now uses the rwidth to give relative width '
7953
7958
'and not absolute width' )
7954
7959
7960
+ if histtype == 'barstacked' and not stacked :
7961
+ stacked = True
7962
+
7955
7963
# Massage 'x' for processing.
7956
7964
# NOTE: Be sure any changes here is also done below to 'weights'
7957
7965
if isinstance (x , np .ndarray ) or not iterable (x [0 ]):
@@ -8037,13 +8045,21 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
8037
8045
hist_kwargs ['new' ] = True
8038
8046
8039
8047
n = []
8040
- for i in xrange (nx ):
8048
+ mlast = bottom
8049
+ # reversed order is necessary so when stacking histogram, first dataset is on top
8050
+ # if histogram isn't stacked, this doesn't make any difference
8051
+ for i in reversed (xrange (nx )):
8041
8052
# this will automatically overwrite bins,
8042
8053
# so that each histogram uses the same bins
8043
8054
m , bins = np .histogram (x [i ], bins , weights = w [i ], ** hist_kwargs )
8055
+ if mlast is None :
8056
+ mlast = np .zeros (len (bins )- 1 , np .int )
8044
8057
if normed :
8045
8058
db = np .diff (bins )
8046
8059
m = (m .astype (float ) / db ) / m .sum ()
8060
+ if stacked :
8061
+ m += mlast
8062
+ mlast [:] = m
8047
8063
n .append (m )
8048
8064
8049
8065
if cumulative :
@@ -8056,6 +8072,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
8056
8072
else :
8057
8073
n = [m [slc ].cumsum ()[slc ] for m in n ]
8058
8074
8075
+ n .reverse () # put them back in the right order
8076
+
8059
8077
patches = []
8060
8078
8061
8079
if histtype .startswith ('bar' ):
@@ -8068,7 +8086,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
8068
8086
else :
8069
8087
dr = 1.0
8070
8088
8071
- if histtype == 'bar' :
8089
+ if histtype == 'bar' and not stacked :
8072
8090
width = dr * totwidth / nx
8073
8091
dw = width
8074
8092
@@ -8077,10 +8095,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
8077
8095
else :
8078
8096
boffset = 0.0
8079
8097
stacked = False
8080
- elif histtype == 'barstacked' :
8098
+ elif histtype == 'barstacked' or stacked :
8081
8099
width = dr * totwidth
8082
8100
boffset , dw = 0.0 , 0.0
8083
- stacked = True
8084
8101
8085
8102
if align == 'mid' or align == 'edge' :
8086
8103
boffset += 0.5 * totwidth
@@ -8093,14 +8110,10 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
8093
8110
_barfunc = self .bar
8094
8111
8095
8112
for m , c in zip (n , color ):
8096
- patch = _barfunc (bins [:- 1 ]+ boffset , m , width , bottom ,
8113
+ patch = _barfunc (bins [:- 1 ]+ boffset , m , width ,
8097
8114
align = 'center' , log = log ,
8098
8115
color = c )
8099
8116
patches .append (patch )
8100
- if stacked :
8101
- if bottom is None :
8102
- bottom = 0.0
8103
- bottom += m
8104
8117
boffset += dw
8105
8118
8106
8119
elif histtype .startswith ('step' ):
@@ -8123,6 +8136,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
8123
8136
else : # orientation == 'vertical'
8124
8137
self .set_yscale ('log' )
8125
8138
8139
+ # If fill kwarg is set, it will be passed to the patch collection,
8140
+ # overriding this
8126
8141
fill = (histtype == 'stepfilled' )
8127
8142
8128
8143
for m , c in zip (n , color ):
0 commit comments