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

Skip to content

Ensure that arguments to quiver() are not matrices. #13089

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
Jan 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: 10 additions & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2178,3 +2178,13 @@ def _check_and_log_subprocess(command, logger, **kwargs):
.format(command, exc.output.decode('utf-8')))
logger.debug(report)
return report


def _check_not_matrix(**kwargs):
"""
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")
28 changes: 13 additions & 15 deletions lib/matplotlib/quiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import weakref

import numpy as np

from numpy import ma
import matplotlib.collections as mcollections
import matplotlib.transforms as transforms
import matplotlib.text as mtext

from matplotlib import cbook, docstring, font_manager
import matplotlib.artist as martist
from matplotlib import docstring
import matplotlib.font_manager as font_manager
from matplotlib.cbook import delete_masked_points
import matplotlib.collections as mcollections
from matplotlib.patches import CirclePolygon
import matplotlib.text as mtext
import matplotlib.transforms as transforms


_quiver_doc = """
Expand Down Expand Up @@ -381,7 +379,7 @@ def contains(self, mouseevent):
# 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] * 5
X = Y = U = V = C = None
args = list(args)

# The use of atleast_1d allows for handling scalar arguments while also
Expand All @@ -390,6 +388,7 @@ def _parse_args(*args):
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]
else:
Expand Down Expand Up @@ -1145,13 +1144,12 @@ def set_UVC(self, U, V, C=None):
self.v = ma.masked_invalid(V, copy=False).ravel()
if C is not None:
c = ma.masked_invalid(C, copy=False).ravel()
x, y, u, v, c = delete_masked_points(self.x.ravel(),
self.y.ravel(),
self.u, self.v, c)
x, y, u, v, c = cbook.delete_masked_points(
self.x.ravel(), self.y.ravel(), self.u, self.v, c)
_check_consistent_shapes(x, y, u, v, c)
else:
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
self.u, self.v)
x, y, u, v = cbook.delete_masked_points(
self.x.ravel(), self.y.ravel(), self.u, self.v)
_check_consistent_shapes(x, y, u, v)

magnitude = np.hypot(u, v)
Expand Down Expand Up @@ -1187,8 +1185,8 @@ def set_offsets(self, xy):
"""
self.x = xy[:, 0]
self.y = xy[:, 1]
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
self.u, self.v)
x, y, u, v = cbook.delete_masked_points(
self.x.ravel(), self.y.ravel(), self.u, self.v)
_check_consistent_shapes(x, y, u, v)
xy = np.column_stack((x, y))
mcollections.PolyCollection.set_offsets(self, xy)
Expand Down
4 changes: 2 additions & 2 deletions tutorials/introductory/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@
# For example, to convert a `pandas.DataFrame` ::
#
# a = pandas.DataFrame(np.random.rand(4,5), columns = list('abcde'))
# a_asndarray = a.values
# a_asarray = a.values
#
# and to covert a `np.matrix` ::
# and to convert a `np.matrix` ::
#
# b = np.matrix([[1,2],[3,4]])
# b_asarray = np.asarray(b)
Expand Down