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

Skip to content

Commit beed2d8

Browse files
authored
Merge pull request #13089 from anntzer/quivermatrix
FIX: Ensure that arguments to quiver() are not matrices.
2 parents aef686b + b275864 commit beed2d8

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
@@ -2118,3 +2118,13 @@ def _check_and_log_subprocess(command, logger, **kwargs):
21182118
.format(command, exc.output.decode('utf-8')))
21192119
logger.debug(report)
21202120
return report
2121+
2122+
2123+
def _check_not_matrix(**kwargs):
2124+
"""
2125+
If any value in *kwargs* is a `np.matrix`, raise a TypeError with the key
2126+
name in its message.
2127+
"""
2128+
for k, v in kwargs.items():
2129+
if isinstance(v, np.matrix):
2130+
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 = """
@@ -380,7 +378,7 @@ def contains(self, mouseevent):
380378
# arguments for doing colored vector plots. Pulling it out here
381379
# allows both Quiver and Barbs to use it
382380
def _parse_args(*args):
383-
X, Y, U, V, C = [None] * 5
381+
X = Y = U = V = C = None
384382
args = list(args)
385383

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

11561154
magnitude = np.hypot(u, v)
@@ -1186,8 +1184,8 @@ def set_offsets(self, xy):
11861184
"""
11871185
self.x = xy[:, 0]
11881186
self.y = xy[:, 1]
1189-
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
1190-
self.u, self.v)
1187+
x, y, u, v = cbook.delete_masked_points(
1188+
self.x.ravel(), self.y.ravel(), self.u, self.v)
11911189
_check_consistent_shapes(x, y, u, v)
11921190
xy = np.column_stack((x, y))
11931191
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)