@@ -6103,13 +6103,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6103
6103
--------
6104
6104
hist2d : 2D histograms
6105
6105
6106
- Notes
6107
- -----
6108
- Until numpy release 1.5, the underlying numpy histogram function was
6109
- incorrect with ``normed=True`` if bin sizes were unequal. MPL
6110
- inherited that error. It is now corrected within MPL when using
6111
- earlier numpy versions.
6112
-
6113
6106
"""
6114
6107
# Avoid shadowing the builtin.
6115
6108
bin_range = range
@@ -6202,38 +6195,37 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6202
6195
else :
6203
6196
hist_kwargs = dict (range = bin_range )
6204
6197
6205
- n = []
6198
+ # List to store all the top coordinates of the histograms
6199
+ tops = []
6206
6200
mlast = None
6201
+ # Loop through datasets
6207
6202
for i in xrange (nx ):
6208
6203
# this will automatically overwrite bins,
6209
6204
# so that each histogram uses the same bins
6210
6205
m , bins = np .histogram (x [i ], bins , weights = w [i ], ** hist_kwargs )
6211
6206
m = m .astype (float ) # causes problems later if it's an int
6212
6207
if mlast is None :
6213
6208
mlast = np .zeros (len (bins )- 1 , m .dtype )
6214
- if density and not stacked :
6215
- db = np .diff (bins )
6216
- m = (m .astype (float ) / db ) / m .sum ()
6217
6209
if stacked :
6218
- if mlast is None :
6219
- mlast = np .zeros (len (bins )- 1 , m .dtype )
6220
6210
m += mlast
6221
6211
mlast [:] = m
6222
- n .append (m )
6212
+ tops .append (m )
6223
6213
6214
+ # If a stacked density plot, normalize so the area of all the stacked
6215
+ # histograms together is 1
6224
6216
if stacked and density :
6225
6217
db = np .diff (bins )
6226
- for m in n :
6227
- m [:] = (m .astype (float ) / db ) / n [- 1 ].sum ()
6218
+ for m in tops :
6219
+ m [:] = (m .astype (float ) / db ) / tops [- 1 ].sum ()
6228
6220
if cumulative :
6229
6221
slc = slice (None )
6230
6222
if cbook .is_numlike (cumulative ) and cumulative < 0 :
6231
6223
slc = slice (None , None , - 1 )
6232
6224
6233
6225
if density :
6234
- n = [(m * np .diff (bins ))[slc ].cumsum ()[slc ] for m in n ]
6226
+ tops = [(m * np .diff (bins ))[slc ].cumsum ()[slc ] for m in tops ]
6235
6227
else :
6236
- n = [m [slc ].cumsum ()[slc ] for m in n ]
6228
+ tops = [m [slc ].cumsum ()[slc ] for m in tops ]
6237
6229
6238
6230
patches = []
6239
6231
@@ -6251,7 +6243,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6251
6243
6252
6244
if rwidth is not None :
6253
6245
dr = np .clip (rwidth , 0 , 1 )
6254
- elif (len (n ) > 1 and
6246
+ elif (len (tops ) > 1 and
6255
6247
((not stacked ) or rcParams ['_internal.classic_mode' ])):
6256
6248
dr = 0.8
6257
6249
else :
@@ -6277,7 +6269,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6277
6269
_barfunc = self .bar
6278
6270
bottom_kwarg = 'bottom'
6279
6271
6280
- for m , c in zip (n , color ):
6272
+ for m , c in zip (tops , color ):
6281
6273
if bottom is None :
6282
6274
bottom = np .zeros (len (m ))
6283
6275
if stacked :
@@ -6321,7 +6313,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6321
6313
# For data that is normed to form a probability density,
6322
6314
# set to minimum data value / logbase
6323
6315
# (gives 1 full tick-label unit for the lowest filled bin)
6324
- ndata = np .array (n )
6316
+ ndata = np .array (tops )
6325
6317
minimum = (np .min (ndata [ndata > 0 ])) / logbase
6326
6318
else :
6327
6319
# For non-normed (density = False) data,
@@ -6344,7 +6336,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6344
6336
fill = (histtype == 'stepfilled' )
6345
6337
6346
6338
xvals , yvals = [], []
6347
- for m in n :
6339
+ for m in tops :
6348
6340
if stacked :
6349
6341
# starting point for drawing polygon
6350
6342
y [0 ] = y [1 ]
@@ -6407,9 +6399,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
6407
6399
p .set_label ('_nolegend_' )
6408
6400
6409
6401
if nx == 1 :
6410
- return n [0 ], bins , cbook .silent_list ('Patch' , patches [0 ])
6402
+ return tops [0 ], bins , cbook .silent_list ('Patch' , patches [0 ])
6411
6403
else :
6412
- return n , bins , cbook .silent_list ('Lists of Patches' , patches )
6404
+ return tops , bins , cbook .silent_list ('Lists of Patches' , patches )
6413
6405
6414
6406
@_preprocess_data (replace_names = ["x" , "y" , "weights" ], label_namer = None )
6415
6407
def hist2d (self , x , y , bins = 10 , range = None , normed = False , weights = None ,
0 commit comments