Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b915ae1

Browse filesBrowse files
committed
ENH: Improve barbs() error message for badly-sized arrays.
With this change, when given masked arrays with incompatible sizes, barbs() will raise a ValueError with a message stating as much. Previously, it would error with a TypeError that did not make it clear at all the source of the problem.
1 parent 1a6a897 commit b915ae1
Copy full SHA for b915ae1

File tree

Expand file treeCollapse file tree

2 files changed

+24
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-0
lines changed

‎lib/matplotlib/quiver.py

Copy file name to clipboardExpand all lines: lib/matplotlib/quiver.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ def _parse_args(*args):
392392
return X, Y, U, V, C
393393

394394

395+
def _check_consistent_shapes(*arrays):
396+
all_shapes = set(a.shape for a in arrays)
397+
if len(all_shapes) != 1:
398+
raise ValueError('The shapes of the passed in arrays do not match.')
399+
400+
395401
class Quiver(mcollections.PolyCollection):
396402
"""
397403
Specialized PolyCollection for arrows.
@@ -1124,9 +1130,11 @@ def set_UVC(self, U, V, C=None):
11241130
x, y, u, v, c = delete_masked_points(self.x.ravel(),
11251131
self.y.ravel(),
11261132
self.u, self.v, c)
1133+
_check_consistent_shapes(x, y, u, v, c)
11271134
else:
11281135
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
11291136
self.u, self.v)
1137+
_check_consistent_shapes(x, y, u, v)
11301138

11311139
magnitude = np.hypot(u, v)
11321140
flags, barbs, halves, empty = self._find_tails(magnitude,
@@ -1161,6 +1169,7 @@ def set_offsets(self, xy):
11611169
self.y = xy[:, 1]
11621170
x, y, u, v = delete_masked_points(self.x.ravel(), self.y.ravel(),
11631171
self.u, self.v)
1172+
_check_consistent_shapes(x, y, u, v)
11641173
xy = np.hstack((x[:, np.newaxis], y[:, np.newaxis]))
11651174
mcollections.PolyCollection.set_offsets(self, xy)
11661175
self.stale = True

‎lib/matplotlib/tests/test_quiver.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_quiver.py
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import print_function
22
import warnings
33
import numpy as np
4+
from nose.tools import raises
45
import sys
56
from matplotlib import pyplot as plt
67
from matplotlib.testing.decorators import cleanup
@@ -133,6 +134,20 @@ def test_barbs():
133134
cmap='viridis')
134135

135136

137+
@cleanup
138+
@raises(ValueError)
139+
def test_bad_masked_sizes():
140+
'Test error handling when given differing sized masked arrays'
141+
x = np.arange(3)
142+
y = np.arange(3)
143+
u = np.ma.array(15. * np.ones((4,)))
144+
v = np.ma.array(15. * np.ones_like(u))
145+
u[1] = np.ma.masked
146+
v[1] = np.ma.masked
147+
fig, ax = plt.subplots()
148+
ax.barbs(x, y, u, v)
149+
150+
136151
if __name__ == '__main__':
137152
import nose
138153
nose.runmodule()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.