@@ -199,6 +199,45 @@ def _hist_bin_auto(x):
199
199
'sturges' : _hist_bin_sturges }
200
200
201
201
202
+ def _ravel_and_check_weights (a , weights ):
203
+ """ Check a and weights have matching shapes, and ravel both """
204
+ a = np .asarray (a )
205
+ if weights is not None :
206
+ weights = np .asarray (weights )
207
+ if weights .shape != a .shape :
208
+ raise ValueError (
209
+ 'weights should have the same shape as a.' )
210
+ weights = weights .ravel ()
211
+ a = a .ravel ()
212
+ return a , weights
213
+
214
+
215
+ def _get_outer_edges (a , range ):
216
+ """
217
+ Determine the outer bin edges to use, from either the data or the range
218
+ argument
219
+ """
220
+ if range is None :
221
+ if a .size == 0 :
222
+ # handle empty arrays. Can't determine range, so use 0-1.
223
+ first_edge , last_edge = 0.0 , 1.0
224
+ else :
225
+ first_edge , last_edge = a .min () + 0.0 , a .max () + 0.0
226
+ else :
227
+ first_edge , last_edge = [mi + 0.0 for mi in range ]
228
+ if first_edge > last_edge :
229
+ raise ValueError (
230
+ 'max must be larger than min in range parameter.' )
231
+ if not np .all (np .isfinite ([first_edge , last_edge ])):
232
+ raise ValueError (
233
+ 'range parameter must be finite.' )
234
+ if first_edge == last_edge :
235
+ first_edge -= 0.5
236
+ last_edge += 0.5
237
+
238
+ return first_edge , last_edge
239
+
240
+
202
241
def histogram (a , bins = 10 , range = None , normed = False , weights = None ,
203
242
density = None ):
204
243
r"""
@@ -414,38 +453,16 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
414
453
>>> plt.show()
415
454
416
455
"""
417
- a = np .asarray (a )
418
- if weights is not None :
419
- weights = np .asarray (weights )
420
- if weights .shape != a .shape :
421
- raise ValueError (
422
- 'weights should have the same shape as a.' )
423
- weights = weights .ravel ()
424
- a = a .ravel ()
425
-
426
- # Do not modify the original value of range so we can check for `None`
427
- if range is None :
428
- if a .size == 0 :
429
- # handle empty arrays. Can't determine range, so use 0-1.
430
- first_edge , last_edge = 0.0 , 1.0
431
- else :
432
- first_edge , last_edge = a .min () + 0.0 , a .max () + 0.0
433
- else :
434
- first_edge , last_edge = [mi + 0.0 for mi in range ]
435
- if first_edge > last_edge :
436
- raise ValueError (
437
- 'max must be larger than min in range parameter.' )
438
- if not np .all (np .isfinite ([first_edge , last_edge ])):
439
- raise ValueError (
440
- 'range parameter must be finite.' )
441
- if first_edge == last_edge :
442
- first_edge -= 0.5
443
- last_edge += 0.5
444
456
445
457
# density overrides the normed keyword
446
458
if density is not None :
447
459
normed = False
448
460
461
+ a , weights = _ravel_and_check_weights (a , weights )
462
+
463
+ # Do not modify the original value of range so we can check for `None`
464
+ first_edge , last_edge = _get_outer_edges (a , range )
465
+
449
466
# parse the overloaded bins argument
450
467
n_equal_bins = None
451
468
bin_edges = None
0 commit comments