@@ -360,31 +360,53 @@ def quiverkey_doc(self):
360360 return self .__init__ .__doc__
361361
362362
363- # This is a helper function that parses out the various combination of
364- # arguments for doing colored vector plots. Pulling it out here
365- # allows both Quiver and Barbs to use it
366- def _parse_args (* args ):
367- X = Y = U = V = C = None
368- args = list (args )
369-
370- # The use of atleast_1d allows for handling scalar arguments while also
371- # keeping masked arrays
372- if len (args ) == 3 or len (args ) == 5 :
373- C = np .atleast_1d (args .pop (- 1 ))
374- V = np .atleast_1d (args .pop (- 1 ))
375- U = np .atleast_1d (args .pop (- 1 ))
376- cbook ._check_not_matrix (U = U , V = V , C = C )
377- if U .ndim == 1 :
378- nr , nc = 1 , U .shape [0 ]
363+ def _parse_args (* args , caller_name = 'function' ):
364+ """
365+ Helper function to parse positional parameters for colored vector plots.
366+
367+ This is currently used for Quiver and Barbs.
368+
369+ Parameters
370+ ----------
371+ *args : list
372+ list of 2-5 arguments. Depending on their number they are parsed to::
373+
374+ U, V
375+ U, V, C
376+ X, Y, U, V
377+ X, Y, U, V, C
378+
379+ caller_name : str
380+ Name of the calling method (used in error messages).
381+ """
382+ X = Y = C = None
383+
384+ len_args = len (args )
385+ if len_args == 2 :
386+ # The use of atleast_1d allows for handling scalar arguments while also
387+ # keeping masked arrays
388+ U , V = np .atleast_1d (* args )
389+ elif len_args == 3 :
390+ U , V , C = np .atleast_1d (* args )
391+ elif len_args == 4 :
392+ X , Y , U , V = np .atleast_1d (* args )
393+ elif len_args == 5 :
394+ X , Y , U , V , C = np .atleast_1d (* args )
379395 else :
380- nr , nc = U .shape
381- if len (args ) == 2 : # remaining after removing U,V,C
382- X , Y = [np .array (a ).ravel () for a in args ]
396+ raise TypeError (f'{ caller_name } takes 2-5 positional arguments but '
397+ f'{ len_args } were given' )
398+
399+ nr , nc = (1 , U .shape [0 ]) if U .ndim == 1 else U .shape
400+
401+ if X is not None :
402+ X = X .ravel ()
403+ Y = Y .ravel ()
383404 if len (X ) == nc and len (Y ) == nr :
384405 X , Y = [a .ravel () for a in np .meshgrid (X , Y )]
385406 else :
386407 indexgrid = np .meshgrid (np .arange (nc ), np .arange (nr ))
387408 X , Y = [np .ravel (a ) for a in indexgrid ]
409+
388410 return X , Y , U , V , C
389411
390412
@@ -426,7 +448,7 @@ def __init__(self, ax, *args,
426448 %s
427449 """
428450 self .ax = ax
429- X , Y , U , V , C = _parse_args (* args )
451+ X , Y , U , V , C = _parse_args (* args , caller_name = 'quiver()' )
430452 self .X = X
431453 self .Y = Y
432454 self .XY = np .column_stack ((X , Y ))
@@ -941,7 +963,7 @@ def __init__(self, ax, *args,
941963 kw ['linewidth' ] = 1
942964
943965 # Parse out the data arrays from the various configurations supported
944- x , y , u , v , c = _parse_args (* args )
966+ x , y , u , v , c = _parse_args (* args , caller_name = 'barbs()' )
945967 self .x = x
946968 self .y = y
947969 xy = np .column_stack ((x , y ))
0 commit comments