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

Skip to content

Check number of positional arguments passed to quiver() #14084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,16 +2148,6 @@ def _check_and_log_subprocess(command, logger, **kwargs):
return report


def _check_not_matrix(**kwargs):
Copy link
Member Author

@timhoffm timhoffm Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was only used in _parse_args, we don't check anywhere else in the code for np.matrix.

np.matrix itself is not recommended anymore and matplotlib usage: Types of inputs states that np.matrix may or may not work.

Therefore I don't see the need to explicitly check for this to give an error message.

Note also, this currently checked only U, V, C but not X, Y.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in #13089 to fix #1558 just in 3.1.0; do we really want to revert that check so quickly?

Also, skipping X and Y was done on purpose, though perhaps that's a little unintuitive.

"""
If any value in *kwargs* is a `np.matrix`, raise a TypeError with the key
name in its message.
"""
for k, v in kwargs.items():
if isinstance(v, np.matrix):
raise TypeError(f"Argument {k!r} cannot be a np.matrix")


def _check_in_list(values, **kwargs):
"""
For each *key, value* pair in *kwargs*, check that *value* is in *values*;
Expand Down
64 changes: 43 additions & 21 deletions lib/matplotlib/quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,31 +360,53 @@ def quiverkey_doc(self):
return self.__init__.__doc__


# This is a helper function that parses out the various combination of
# arguments for doing colored vector plots. Pulling it out here
# allows both Quiver and Barbs to use it
def _parse_args(*args):
X = Y = U = V = C = None
args = list(args)

# The use of atleast_1d allows for handling scalar arguments while also
# keeping masked arrays
if len(args) == 3 or len(args) == 5:
C = np.atleast_1d(args.pop(-1))
V = np.atleast_1d(args.pop(-1))
U = np.atleast_1d(args.pop(-1))
cbook._check_not_matrix(U=U, V=V, C=C)
if U.ndim == 1:
nr, nc = 1, U.shape[0]
def _parse_args(*args, caller_name='function'):
"""
Helper function to parse positional parameters for colored vector plots.

This is currently used for Quiver and Barbs.

Parameters
----------
*args : list
list of 2-5 arguments. Depending on their number they are parsed to::

U, V
U, V, C
X, Y, U, V
X, Y, U, V, C

caller_name : str
Name of the calling method (used in error messages).
"""
X = Y = C = None

len_args = len(args)
if len_args == 2:
# The use of atleast_1d allows for handling scalar arguments while also
# keeping masked arrays
U, V = np.atleast_1d(*args)
elif len_args == 3:
U, V, C = np.atleast_1d(*args)
elif len_args == 4:
X, Y, U, V = np.atleast_1d(*args)
elif len_args == 5:
X, Y, U, V, C = np.atleast_1d(*args)
else:
nr, nc = U.shape
if len(args) == 2: # remaining after removing U,V,C
X, Y = [np.array(a).ravel() for a in args]
raise TypeError(f'{caller_name} takes 2-5 positional arguments but '
f'{len_args} were given')

nr, nc = (1, U.shape[0]) if U.ndim == 1 else U.shape

if X is not None:
X = X.ravel()
Y = Y.ravel()
if len(X) == nc and len(Y) == nr:
X, Y = [a.ravel() for a in np.meshgrid(X, Y)]
else:
indexgrid = np.meshgrid(np.arange(nc), np.arange(nr))
X, Y = [np.ravel(a) for a in indexgrid]

return X, Y, U, V, C


Expand Down Expand Up @@ -426,7 +448,7 @@ def __init__(self, ax, *args,
%s
"""
self.ax = ax
X, Y, U, V, C = _parse_args(*args)
X, Y, U, V, C = _parse_args(*args, caller_name='quiver()')
self.X = X
self.Y = Y
self.XY = np.column_stack((X, Y))
Expand Down Expand Up @@ -941,7 +963,7 @@ def __init__(self, ax, *args,
kw['linewidth'] = 1

# Parse out the data arrays from the various configurations supported
x, y, u, v, c = _parse_args(*args)
x, y, u, v, c = _parse_args(*args, caller_name='barbs()')
self.x = x
self.y = y
xy = np.column_stack((x, y))
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def test_quiver_key_memory_leak():
assert sys.getrefcount(qk) == 2


def test_quiver_number_of_args():
X = [1, 2]
with pytest.raises(TypeError,
match='takes 2-5 positional arguments but 1 were given'):
plt.quiver(X)
with pytest.raises(TypeError,
match='takes 2-5 positional arguments but 6 were given'):
plt.quiver(X, X, X, X, X, X)


def test_no_warnings():
fig, ax = plt.subplots()

Expand Down