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

Skip to content

Commit 0f85365

Browse files
authored
Merge pull request #14084 from timhoffm/quiver-check-nargs
Check number of positional arguments passed to quiver()
2 parents 48bc2be + 370cc5b commit 0f85365

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

lib/matplotlib/cbook/__init__.py

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

21722172

2173-
def _check_not_matrix(**kwargs):
2174-
"""
2175-
If any value in *kwargs* is a `np.matrix`, raise a TypeError with the key
2176-
name in its message.
2177-
"""
2178-
for k, v in kwargs.items():
2179-
if isinstance(v, np.matrix):
2180-
raise TypeError(f"Argument {k!r} cannot be a np.matrix")
2181-
2182-
21832173
def _check_in_list(values, **kwargs):
21842174
"""
21852175
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
@@ -359,31 +359,53 @@ def quiverkey_doc(self):
359359
return self.__init__.__doc__
360360

361361

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)
378394
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()
382403
if len(X) == nc and len(Y) == nr:
383404
X, Y = [a.ravel() for a in np.meshgrid(X, Y)]
384405
else:
385406
indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))
386407
X, Y = [np.ravel(a) for a in indexgrid]
408+
387409
return X, Y, U, V, C
388410

389411

@@ -425,7 +447,7 @@ def __init__(self, ax, *args,
425447
%s
426448
"""
427449
self.ax = ax
428-
X, Y, U, V, C = _parse_args(*args)
450+
X, Y, U, V, C = _parse_args(*args, caller_name='quiver()')
429451
self.X = X
430452
self.Y = Y
431453
self.XY = np.column_stack((X, Y))
@@ -938,7 +960,7 @@ def __init__(self, ax, *args,
938960
kw['linewidth'] = 1
939961

940962
# 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()')
942964
self.x = x
943965
self.y = y
944966
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)