@@ -5875,6 +5875,40 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
5875
5875
.. plot:: mpl_examples/statistics/histogram_demo_features.py
5876
5876
5877
5877
"""
5878
+ def _normalize_input (inp , ename = 'input' ):
5879
+ """Normalize 1 or 2d input into list of np.ndarray or
5880
+ a single 2D np.ndarray.
5881
+
5882
+ Parameters
5883
+ ----------
5884
+ inp : iterable
5885
+ ename : str, optional
5886
+ Name to use is ValueError if can not be normalized
5887
+
5888
+ """
5889
+ if isinstance (x , np .ndarray ) or not iterable (next (iter (inp ))):
5890
+ # TODO: support masked arrays;
5891
+ inp = np .asarray (inp )
5892
+ if inp .ndim == 2 :
5893
+ # 2-D input with columns as datasets; switch to rows
5894
+ inp = inp .T
5895
+ elif inp .ndim == 1 :
5896
+ # new view, single row
5897
+ inp = inp .reshape (1 , inp .shape [0 ])
5898
+ else :
5899
+ raise ValueError (
5900
+ "{ename} must be 1D or 2D" .format (ename = ename ))
5901
+ if inp .shape [1 ] < inp .shape [0 ]:
5902
+ warnings .warn (
5903
+ '2D hist input should be nsamples x nvariables;\n '
5904
+ 'this looks transposed '
5905
+ '(shape is %d x %d)' % inp .shape [::- 1 ])
5906
+ else :
5907
+ # multiple hist with data of different length
5908
+ inp = [np .asarray (xi ) for xi in inp ]
5909
+
5910
+ return inp
5911
+
5878
5912
if not self ._hold :
5879
5913
self .cla ()
5880
5914
@@ -5918,28 +5952,26 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
5918
5952
input_empty = len (flat ) == 0
5919
5953
5920
5954
# Massage 'x' for processing.
5921
- # NOTE: Be sure any changes here is also done below to 'weights'
5922
5955
if input_empty :
5923
5956
x = np .array ([[]])
5924
- elif isinstance (x , np .ndarray ) or not iterable (x [0 ]):
5925
- # TODO: support masked arrays;
5926
- x = np .asarray (x )
5927
- if x .ndim == 2 :
5928
- x = x .T # 2-D input with columns as datasets; switch to rows
5929
- elif x .ndim == 1 :
5930
- x = x .reshape (1 , x .shape [0 ]) # new view, single row
5931
- else :
5932
- raise ValueError ("x must be 1D or 2D" )
5933
- if x .shape [1 ] < x .shape [0 ]:
5934
- warnings .warn (
5935
- '2D hist input should be nsamples x nvariables;\n '
5936
- 'this looks transposed (shape is %d x %d)' % x .shape [::- 1 ])
5937
5957
else :
5938
- # multiple hist with data of different length
5939
- x = [np .asarray (xi ) for xi in x ]
5940
-
5958
+ x = _normalize_input (x , 'x' )
5941
5959
nx = len (x ) # number of datasets
5942
5960
5961
+ # We need to do to 'weights' what was done to 'x'
5962
+ if weights is not None :
5963
+ w = _normalize_input (weights , 'weights' )
5964
+ else :
5965
+ w = [None ]* nx
5966
+
5967
+ if len (w ) != nx :
5968
+ raise ValueError ('weights should have the same shape as x' )
5969
+
5970
+ for xi , wi in zip (x , w ):
5971
+ if wi is not None and len (wi ) != len (xi ):
5972
+ raise ValueError (
5973
+ 'weights should have the same shape as x' )
5974
+
5943
5975
if color is None and 'color' in self ._get_lines ._prop_keys :
5944
5976
color = [six .next (self ._get_lines .prop_cycler )['color' ]
5945
5977
for i in xrange (nx )]
@@ -5948,28 +5980,6 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
5948
5980
if len (color ) != nx :
5949
5981
raise ValueError ("color kwarg must have one color per dataset" )
5950
5982
5951
- # We need to do to 'weights' what was done to 'x'
5952
- if weights is not None :
5953
- if isinstance (weights , np .ndarray ) or not iterable (weights [0 ]):
5954
- w = np .array (weights )
5955
- if w .ndim == 2 :
5956
- w = w .T
5957
- elif w .ndim == 1 :
5958
- w .shape = (1 , w .shape [0 ])
5959
- else :
5960
- raise ValueError ("weights must be 1D or 2D" )
5961
- else :
5962
- w = [np .asarray (wi ) for wi in weights ]
5963
-
5964
- if len (w ) != nx :
5965
- raise ValueError ('weights should have the same shape as x' )
5966
- for i in xrange (nx ):
5967
- if len (w [i ]) != len (x [i ]):
5968
- raise ValueError (
5969
- 'weights should have the same shape as x' )
5970
- else :
5971
- w = [None ]* nx
5972
-
5973
5983
# Save the datalimits for the same reason:
5974
5984
_saved_bounds = self .dataLim .bounds
5975
5985
0 commit comments