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