Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 370cc5b

Browse files
committed
Check number of positional arguments passed to quiver()
1 parent c1a3c03 commit 370cc5b

3 files changed

Lines changed: 53 additions & 31 deletions

File tree

lib/matplotlib/cbook/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,16 +2148,6 @@ def _check_and_log_subprocess(command, logger, **kwargs):
21482148
return report
21492149

21502150

2151-
def _check_not_matrix(**kwargs):
2152-
"""
2153-
If any value in *kwargs* is a `np.matrix`, raise a TypeError with the key
2154-
name in its message.
2155-
"""
2156-
for k, v in kwargs.items():
2157-
if isinstance(v, np.matrix):
2158-
raise TypeError(f"Argument {k!r} cannot be a np.matrix")
2159-
2160-
21612151
def _check_in_list(values, **kwargs):
21622152
"""
21632153
For each *key, value* pair in *kwargs*, check that *value* is in *values*;

lib/matplotlib/quiver.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

lib/matplotlib/tests/test_quiver.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ def test_quiver_key_memory_leak():
4141
assert sys.getrefcount(qk) == 2
4242

4343

44+
def test_quiver_number_of_args():
45+
X = [1, 2]
46+
with pytest.raises(TypeError,
47+
match='takes 2-5 positional arguments but 1 were given'):
48+
plt.quiver(X)
49+
with pytest.raises(TypeError,
50+
match='takes 2-5 positional arguments but 6 were given'):
51+
plt.quiver(X, X, X, X, X, X)
52+
53+
4454
def test_no_warnings():
4555
fig, ax = plt.subplots()
4656

0 commit comments

Comments
 (0)