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

Skip to content

Commit b275864

Browse files
committed
Ensure that arguments to quiver() are not matrices.
1 parent 354a430 commit b275864

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,3 +2178,13 @@ def _check_and_log_subprocess(command, logger, **kwargs):
21782178
.format(command, exc.output.decode('utf-8')))
21792179
logger.debug(report)
21802180
return report
2181+
2182+
2183+
def _check_not_matrix(**kwargs):
2184+
"""
2185+
If any value in *kwargs* is a `np.matrix`, raise a TypeError with the key
2186+
name in its message.
2187+
"""
2188+
for k, v in kwargs.items():
2189+
if isinstance(v, np.matrix):
2190+
raise TypeError(f"Argument {k!r} cannot be a np.matrix")

lib/matplotlib/quiver.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
import weakref
1919

2020
import numpy as np
21-
2221
from numpy import ma
23-
import matplotlib.collections as mcollections
24-
import matplotlib.transforms as transforms
25-
import matplotlib.text as mtext
22+
23+
from matplotlib import cbook, docstring, font_manager
2624
import matplotlib.artist as martist
27-
from matplotlib import docstring
28-
import matplotlib.font_manager as font_manager
29-
from matplotlib.cbook import delete_masked_points
25+
import matplotlib.collections as mcollections
3026
from matplotlib.patches import CirclePolygon
27+
import matplotlib.text as mtext
28+
import matplotlib.transforms as transforms
3129

3230

3331
_quiver_doc = """
@@ -381,7 +379,7 @@ def contains(self, mouseevent):
381379
# arguments for doing colored vector plots. Pulling it out here
382380
# allows both Quiver and Barbs to use it
383381
def _parse_args(*args):
384-
X, Y, U, V, C = [None] * 5
382+
X = Y = U = V = C = None
385383
args = list(args)
386384

387385
# The use of atleast_1d allows for handling scalar arguments while also
@@ -390,6 +388,7 @@ def _parse_args(*args):
390388
C = np.atleast_1d(args.pop(-1))
391389
V = np.atleast_1d(args.pop(-1))
392390
U = np.atleast_1d(args.pop(-1))
391+
cbook._check_not_matrix(U=U, V=V, C=C)
393392
if U.ndim == 1:
394393
nr, nc = 1, U.shape[0]
395394
else:
@@ -1145,13 +1144,12 @@ def set_UVC(self, U, V, C=None):
11451144
self.v = ma.masked_invalid(V, copy=False).ravel()
11461145
if C is not None:
11471146
c = ma.masked_invalid(C, copy=False).ravel()
1148-
x, y, u, v, c = delete_masked_points(self.x.ravel(),
1149-
self.y.ravel(),
1150-
self.u, self.v, c)
1147+
x, y, u, v, c = cbook.delete_masked_points(
1148+
self.x.ravel(), self.y.ravel(), self.u, self.v, c)
11511149
_check_consistent_shapes(x, y, u, v, c)
11521150
else:
1153-
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
1154-
self.u, self.v)
1151+
x, y, u, v = cbook.delete_masked_points(
1152+
self.x.ravel(), self.y.ravel(), self.u, self.v)
11551153
_check_consistent_shapes(x, y, u, v)
11561154

11571155
magnitude = np.hypot(u, v)
@@ -1187,8 +1185,8 @@ def set_offsets(self, xy):
11871185
"""
11881186
self.x = xy[:, 0]
11891187
self.y = xy[:, 1]
1190-
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
1191-
self.u, self.v)
1188+
x, y, u, v = cbook.delete_masked_points(
1189+
self.x.ravel(), self.y.ravel(), self.u, self.v)
11921190
_check_consistent_shapes(x, y, u, v)
11931191
xy = np.column_stack((x, y))
11941192
mcollections.PolyCollection.set_offsets(self, xy)

tutorials/introductory/usage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@
137137
# For example, to convert a `pandas.DataFrame` ::
138138
#
139139
# a = pandas.DataFrame(np.random.rand(4,5), columns = list('abcde'))
140-
# a_asndarray = a.values
140+
# a_asarray = a.values
141141
#
142-
# and to covert a `np.matrix` ::
142+
# and to convert a `np.matrix` ::
143143
#
144144
# b = np.matrix([[1,2],[3,4]])
145145
# b_asarray = np.asarray(b)

0 commit comments

Comments
 (0)