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 2785adc

Browse filesBrowse files
authored
Merge pull request #22329 from QuLogic/line-seq
Enforce that Line data modifications are sequences
2 parents 488962a + 1e602a6 commit 2785adc
Copy full SHA for 2785adc

File tree

8 files changed

+50
-13
lines changed
Filter options

8 files changed

+50
-13
lines changed

‎examples/animation/multiple_axes.py

Copy file name to clipboardExpand all lines: examples/animation/multiple_axes.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@
4848

4949

5050
def animate(i):
51-
pos = np.cos(i), np.sin(i)
52-
point.set_data(*pos)
5351
x = np.linspace(0, i, int(i * 25 / np.pi))
5452
sine.set_data(x, np.sin(x))
55-
con.xy1 = pos
56-
con.xy2 = i, pos[1]
53+
x, y = np.cos(i), np.sin(i)
54+
point.set_data([x], [y])
55+
con.xy1 = x, y
56+
con.xy2 = i, y
5757
return point, sine, con
5858

5959

‎lib/matplotlib/lines.py

Copy file name to clipboardExpand all lines: lib/matplotlib/lines.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ def set_xdata(self, x):
12741274
----------
12751275
x : 1D array
12761276
"""
1277+
if not np.iterable(x):
1278+
raise RuntimeError('x must be a sequence')
12771279
self._xorig = copy.copy(x)
12781280
self._invalidx = True
12791281
self.stale = True
@@ -1286,6 +1288,8 @@ def set_ydata(self, y):
12861288
----------
12871289
y : 1D array
12881290
"""
1291+
if not np.iterable(y):
1292+
raise RuntimeError('y must be a sequence')
12891293
self._yorig = copy.copy(y)
12901294
self._invalidy = True
12911295
self.stale = True

‎lib/matplotlib/tests/test_lines.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_lines.py
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ def test_set_line_coll_dash():
8383
ax.contour(np.random.randn(20, 30), linestyles=[(0, (3, 3))])
8484

8585

86+
def test_invalid_line_data():
87+
with pytest.raises(RuntimeError, match='xdata must be'):
88+
mlines.Line2D(0, [])
89+
with pytest.raises(RuntimeError, match='ydata must be'):
90+
mlines.Line2D([], 1)
91+
92+
line = mlines.Line2D([], [])
93+
with pytest.raises(RuntimeError, match='x must be'):
94+
line.set_xdata(0)
95+
with pytest.raises(RuntimeError, match='y must be'):
96+
line.set_ydata(0)
97+
98+
8699
@image_comparison(['line_dashes'], remove_text=True)
87100
def test_line_dashes():
88101
fig, ax = plt.subplots()

‎lib/matplotlib/tests/test_widgets.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_widgets.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ def mean(vmin, vmax):
871871
# Return mean of values in x between *vmin* and *vmax*
872872
indmin, indmax = np.searchsorted(x, (vmin, vmax))
873873
v = values[indmin:indmax].mean()
874-
ln2.set_data(x, v)
874+
ln2.set_data(x, np.full_like(x, v))
875875

876876
span = widgets.SpanSelector(ax, mean, direction='horizontal',
877877
onmove_callback=mean,
@@ -888,7 +888,7 @@ def mean(vmin, vmax):
888888
assert span._get_animated_artists() == (ln, ln2)
889889
assert ln.stale is False
890890
assert ln2.stale
891-
assert ln2.get_ydata() == 0.9547335049088455
891+
assert_allclose(ln2.get_ydata(), 0.9547335049088455)
892892
span.update()
893893
assert ln2.stale is False
894894

@@ -901,7 +901,7 @@ def mean(vmin, vmax):
901901
do_event(span, 'onmove', xdata=move_data[0], ydata=move_data[1], button=1)
902902
assert ln.stale is False
903903
assert ln2.stale
904-
assert ln2.get_ydata() == -0.9424150707548072
904+
assert_allclose(ln2.get_ydata(), -0.9424150707548072)
905905
do_event(span, 'release', xdata=release_data[0],
906906
ydata=release_data[1], button=1)
907907
assert ln2.stale is False

‎lib/matplotlib/widgets.py

Copy file name to clipboardExpand all lines: lib/matplotlib/widgets.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3426,7 +3426,8 @@ def extents(self, extents):
34263426
# Update displayed handles
34273427
self._corner_handles.set_data(*self.corners)
34283428
self._edge_handles.set_data(*self.edge_centers)
3429-
self._center_handle.set_data(*self.center)
3429+
x, y = self.center
3430+
self._center_handle.set_data([x], [y])
34303431
self.set_visible(self._visible)
34313432
self.update()
34323433

‎lib/mpl_toolkits/mplot3d/art3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/mplot3d/art3d.py
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def __init__(self, xs, ys, zs, *args, **kwargs):
200200
Additional arguments are passed onto :func:`~matplotlib.lines.Line2D`.
201201
"""
202202
super().__init__([], [], *args, **kwargs)
203-
self._verts3d = xs, ys, zs
203+
self.set_data_3d(xs, ys, zs)
204204

205205
def set_3d_properties(self, zs=0, zdir='z'):
206206
"""
@@ -240,9 +240,11 @@ def set_data_3d(self, *args):
240240
Accepts x, y, z arguments or a single array-like (x, y, z)
241241
"""
242242
if len(args) == 1:
243-
self._verts3d = args[0]
244-
else:
245-
self._verts3d = args
243+
args = args[0]
244+
for name, xyz in zip('xyz', args):
245+
if not np.iterable(xyz):
246+
raise RuntimeError(f'{name} must be a sequence')
247+
self._verts3d = args
246248
self.stale = True
247249

248250
def get_data_3d(self):

‎lib/mpl_toolkits/mplot3d/axis3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/mplot3d/axis3d.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _tick_update_position(tick, tickxs, tickys, labelpos):
4747
tick.tick1line.set_linestyle('-')
4848
tick.tick1line.set_marker('')
4949
tick.tick1line.set_data(tickxs, tickys)
50-
tick.gridline.set_data(0, 0)
50+
tick.gridline.set_data([0], [0])
5151

5252

5353
class Axis(maxis.XAxis):

‎lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ def test_plot_scalar(fig_test, fig_ref):
310310
ax2.plot(1, 1, "o")
311311

312312

313+
def test_invalid_line_data():
314+
with pytest.raises(RuntimeError, match='x must be'):
315+
art3d.Line3D(0, [], [])
316+
with pytest.raises(RuntimeError, match='y must be'):
317+
art3d.Line3D([], 0, [])
318+
with pytest.raises(RuntimeError, match='z must be'):
319+
art3d.Line3D([], [], 0)
320+
321+
line = art3d.Line3D([], [], [])
322+
with pytest.raises(RuntimeError, match='x must be'):
323+
line.set_data_3d(0, [], [])
324+
with pytest.raises(RuntimeError, match='y must be'):
325+
line.set_data_3d([], 0, [])
326+
with pytest.raises(RuntimeError, match='z must be'):
327+
line.set_data_3d([], [], 0)
328+
329+
313330
@mpl3d_image_comparison(['mixedsubplot.png'])
314331
def test_mixedsubplots():
315332
def f(t):

0 commit comments

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