@@ -2331,11 +2331,11 @@ def broken_barh(self, xranges, yrange, **kwargs):
23312331 """
23322332 # process the unit information
23332333 if len (xranges ):
2334- xdata = six . next ( iter ( xranges ) )
2334+ xdata = cbook . safe_first_element ( xranges )
23352335 else :
23362336 xdata = None
23372337 if len (yrange ):
2338- ydata = six . next ( iter ( yrange ) )
2338+ ydata = cbook . safe_first_element ( yrange )
23392339 else :
23402340 ydata = None
23412341 self ._process_unit_info (xdata = xdata ,
@@ -3016,7 +3016,7 @@ def xywhere(xs, ys, mask):
30163016
30173017 if ecolor is None :
30183018 if l0 is None and 'color' in self ._get_lines ._prop_keys :
3019- ecolor = six . next (self ._get_lines .prop_cycler )['color' ]
3019+ ecolor = next (self ._get_lines .prop_cycler )['color' ]
30203020 else :
30213021 ecolor = l0 .get_color ()
30223022
@@ -5875,6 +5875,41 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
58755875 .. plot:: mpl_examples/statistics/histogram_demo_features.py
58765876
58775877 """
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 in ValueError if `inp` can not be normalized
5887+
5888+ """
5889+ if (isinstance (x , np .ndarray ) or
5890+ not iterable (cbook .safe_first_element (inp ))):
5891+ # TODO: support masked arrays;
5892+ inp = np .asarray (inp )
5893+ if inp .ndim == 2 :
5894+ # 2-D input with columns as datasets; switch to rows
5895+ inp = inp .T
5896+ elif inp .ndim == 1 :
5897+ # new view, single row
5898+ inp = inp .reshape (1 , inp .shape [0 ])
5899+ else :
5900+ raise ValueError (
5901+ "{ename} must be 1D or 2D" .format (ename = ename ))
5902+ if inp .shape [1 ] < inp .shape [0 ]:
5903+ warnings .warn (
5904+ '2D hist input should be nsamples x nvariables;\n '
5905+ 'this looks transposed '
5906+ '(shape is %d x %d)' % inp .shape [::- 1 ])
5907+ else :
5908+ # multiple hist with data of different length
5909+ inp = [np .asarray (xi ) for xi in inp ]
5910+
5911+ return inp
5912+
58785913 if not self ._hold :
58795914 self .cla ()
58805915
@@ -5918,58 +5953,34 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
59185953 input_empty = len (flat ) == 0
59195954
59205955 # Massage 'x' for processing.
5921- # NOTE: Be sure any changes here is also done below to 'weights'
59225956 if input_empty :
59235957 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 ])
59375958 else :
5938- # multiple hist with data of different length
5939- x = [np .asarray (xi ) for xi in x ]
5940-
5959+ x = _normalize_input (x , 'x' )
59415960 nx = len (x ) # number of datasets
59425961
5962+ # We need to do to 'weights' what was done to 'x'
5963+ if weights is not None :
5964+ w = _normalize_input (weights , 'weights' )
5965+ else :
5966+ w = [None ]* nx
5967+
5968+ if len (w ) != nx :
5969+ raise ValueError ('weights should have the same shape as x' )
5970+
5971+ for xi , wi in zip (x , w ):
5972+ if wi is not None and len (wi ) != len (xi ):
5973+ raise ValueError (
5974+ 'weights should have the same shape as x' )
5975+
59435976 if color is None and 'color' in self ._get_lines ._prop_keys :
5944- color = [six . next (self ._get_lines .prop_cycler )['color' ]
5977+ color = [next (self ._get_lines .prop_cycler )['color' ]
59455978 for i in xrange (nx )]
59465979 else :
59475980 color = mcolors .colorConverter .to_rgba_array (color )
59485981 if len (color ) != nx :
59495982 raise ValueError ("color kwarg must have one color per dataset" )
59505983
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-
59735984 # Save the datalimits for the same reason:
59745985 _saved_bounds = self .dataLim .bounds
59755986
@@ -5985,7 +5996,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
59855996 xmax = max (xmax , xi .max ())
59865997 bin_range = (xmin , xmax )
59875998
5988- #hist_kwargs = dict(range=range, normed=bool(normed))
5999+ # hist_kwargs = dict(range=range, normed=bool(normed))
59896000 # We will handle the normed kwarg within mpl until we
59906001 # get to the point of requiring numpy >= 1.5.
59916002 hist_kwargs = dict (range = bin_range )
0 commit comments