@@ -359,31 +359,53 @@ def quiverkey_doc(self):
359
359
return self .__init__ .__doc__
360
360
361
361
362
- # This is a helper function that parses out the various combination of
363
- # arguments for doing colored vector plots. Pulling it out here
364
- # allows both Quiver and Barbs to use it
365
- def _parse_args (* args ):
366
- X = Y = U = V = C = None
367
- args = list (args )
368
-
369
- # The use of atleast_1d allows for handling scalar arguments while also
370
- # keeping masked arrays
371
- if len (args ) == 3 or len (args ) == 5 :
372
- C = np .atleast_1d (args .pop (- 1 ))
373
- V = np .atleast_1d (args .pop (- 1 ))
374
- U = np .atleast_1d (args .pop (- 1 ))
375
- cbook ._check_not_matrix (U = U , V = V , C = C )
376
- if U .ndim == 1 :
377
- nr , nc = 1 , U .shape [0 ]
362
+ def _parse_args (* args , caller_name = 'function' ):
363
+ """
364
+ Helper function to parse positional parameters for colored vector plots.
365
+
366
+ This is currently used for Quiver and Barbs.
367
+
368
+ Parameters
369
+ ----------
370
+ *args : list
371
+ list of 2-5 arguments. Depending on their number they are parsed to::
372
+
373
+ U, V
374
+ U, V, C
375
+ X, Y, U, V
376
+ X, Y, U, V, C
377
+
378
+ caller_name : str
379
+ Name of the calling method (used in error messages).
380
+ """
381
+ X = Y = C = None
382
+
383
+ len_args = len (args )
384
+ if len_args == 2 :
385
+ # The use of atleast_1d allows for handling scalar arguments while also
386
+ # keeping masked arrays
387
+ U , V = np .atleast_1d (* args )
388
+ elif len_args == 3 :
389
+ U , V , C = np .atleast_1d (* args )
390
+ elif len_args == 4 :
391
+ X , Y , U , V = np .atleast_1d (* args )
392
+ elif len_args == 5 :
393
+ X , Y , U , V , C = np .atleast_1d (* args )
378
394
else :
379
- nr , nc = U .shape
380
- if len (args ) == 2 : # remaining after removing U,V,C
381
- X , Y = [np .array (a ).ravel () for a in args ]
395
+ raise TypeError (f'{ caller_name } takes 2-5 positional arguments but '
396
+ f'{ len_args } were given' )
397
+
398
+ nr , nc = (1 , U .shape [0 ]) if U .ndim == 1 else U .shape
399
+
400
+ if X is not None :
401
+ X = X .ravel ()
402
+ Y = Y .ravel ()
382
403
if len (X ) == nc and len (Y ) == nr :
383
404
X , Y = [a .ravel () for a in np .meshgrid (X , Y )]
384
405
else :
385
406
indexgrid = np .meshgrid (np .arange (nc ), np .arange (nr ))
386
407
X , Y = [np .ravel (a ) for a in indexgrid ]
408
+
387
409
return X , Y , U , V , C
388
410
389
411
@@ -425,7 +447,7 @@ def __init__(self, ax, *args,
425
447
%s
426
448
"""
427
449
self .ax = ax
428
- X , Y , U , V , C = _parse_args (* args )
450
+ X , Y , U , V , C = _parse_args (* args , caller_name = 'quiver()' )
429
451
self .X = X
430
452
self .Y = Y
431
453
self .XY = np .column_stack ((X , Y ))
@@ -938,7 +960,7 @@ def __init__(self, ax, *args,
938
960
kw ['linewidth' ] = 1
939
961
940
962
# Parse out the data arrays from the various configurations supported
941
- x , y , u , v , c = _parse_args (* args )
963
+ x , y , u , v , c = _parse_args (* args , caller_name = 'barbs()' )
942
964
self .x = x
943
965
self .y = y
944
966
xy = np .column_stack ((x , y ))
0 commit comments