From b275864943a32b3cd1c377eb5dbec81ea22f69a7 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 3 Jan 2019 14:26:27 +0100 Subject: [PATCH] Ensure that arguments to quiver() are not matrices. --- lib/matplotlib/cbook/__init__.py | 10 ++++++++++ lib/matplotlib/quiver.py | 28 +++++++++++++--------------- tutorials/introductory/usage.py | 4 ++-- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 306cd6812b97..810028b22874 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -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") diff --git a/lib/matplotlib/quiver.py b/lib/matplotlib/quiver.py index 65a8f48411b7..8ff4920015d4 100644 --- a/lib/matplotlib/quiver.py +++ b/lib/matplotlib/quiver.py @@ -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 = """ @@ -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 @@ -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: @@ -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) @@ -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) diff --git a/tutorials/introductory/usage.py b/tutorials/introductory/usage.py index 5777a9be6197..8770359e3594 100644 --- a/tutorials/introductory/usage.py +++ b/tutorials/introductory/usage.py @@ -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)