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

Skip to content

Enforce that Line data modifications are sequences #22329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/animation/multiple_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@


def animate(i):
pos = np.cos(i), np.sin(i)
point.set_data(*pos)
x = np.linspace(0, i, int(i * 25 / np.pi))
sine.set_data(x, np.sin(x))
con.xy1 = pos
con.xy2 = i, pos[1]
x, y = np.cos(i), np.sin(i)
point.set_data([x], [y])
con.xy1 = x, y
con.xy2 = i, y
return point, sine, con


Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,8 @@ def set_xdata(self, x):
----------
x : 1D array
"""
if not np.iterable(x):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You can remove the corresponding checks in __init__.
  • Do you want to check instead np.ndim(x) == 1? (A 2D input doesn't really help either...)

raise RuntimeError('x must be a sequence')
self._xorig = copy.copy(x)
self._invalidx = True
self.stale = True
Expand All @@ -1286,6 +1288,8 @@ def set_ydata(self, y):
----------
y : 1D array
"""
if not np.iterable(y):
raise RuntimeError('y must be a sequence')
self._yorig = copy.copy(y)
self._invalidy = True
self.stale = True
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ def test_set_line_coll_dash():
ax.contour(np.random.randn(20, 30), linestyles=[(0, (3, 3))])


def test_invalid_line_data():
with pytest.raises(RuntimeError, match='xdata must be'):
mlines.Line2D(0, [])
with pytest.raises(RuntimeError, match='ydata must be'):
mlines.Line2D([], 1)

line = mlines.Line2D([], [])
with pytest.raises(RuntimeError, match='x must be'):
line.set_xdata(0)
with pytest.raises(RuntimeError, match='y must be'):
line.set_ydata(0)


@image_comparison(['line_dashes'], remove_text=True)
def test_line_dashes():
fig, ax = plt.subplots()
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ def mean(vmin, vmax):
# Return mean of values in x between *vmin* and *vmax*
indmin, indmax = np.searchsorted(x, (vmin, vmax))
v = values[indmin:indmax].mean()
ln2.set_data(x, v)
ln2.set_data(x, np.full_like(x, v))

span = widgets.SpanSelector(ax, mean, direction='horizontal',
onmove_callback=mean,
Expand All @@ -888,7 +888,7 @@ def mean(vmin, vmax):
assert span._get_animated_artists() == (ln, ln2)
assert ln.stale is False
assert ln2.stale
assert ln2.get_ydata() == 0.9547335049088455
assert_allclose(ln2.get_ydata(), 0.9547335049088455)
span.update()
assert ln2.stale is False

Expand All @@ -901,7 +901,7 @@ def mean(vmin, vmax):
do_event(span, 'onmove', xdata=move_data[0], ydata=move_data[1], button=1)
assert ln.stale is False
assert ln2.stale
assert ln2.get_ydata() == -0.9424150707548072
assert_allclose(ln2.get_ydata(), -0.9424150707548072)
do_event(span, 'release', xdata=release_data[0],
ydata=release_data[1], button=1)
assert ln2.stale is False
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3426,7 +3426,8 @@ def extents(self, extents):
# Update displayed handles
self._corner_handles.set_data(*self.corners)
self._edge_handles.set_data(*self.edge_centers)
self._center_handle.set_data(*self.center)
x, y = self.center
self._center_handle.set_data([x], [y])
self.set_visible(self._visible)
self.update()

Expand Down
10 changes: 6 additions & 4 deletions lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def __init__(self, xs, ys, zs, *args, **kwargs):
Additional arguments are passed onto :func:`~matplotlib.lines.Line2D`.
"""
super().__init__([], [], *args, **kwargs)
self._verts3d = xs, ys, zs
self.set_data_3d(xs, ys, zs)

def set_3d_properties(self, zs=0, zdir='z'):
"""
Expand Down Expand Up @@ -240,9 +240,11 @@ def set_data_3d(self, *args):
Accepts x, y, z arguments or a single array-like (x, y, z)
"""
if len(args) == 1:
self._verts3d = args[0]
else:
self._verts3d = args
args = args[0]
for name, xyz in zip('xyz', args):
if not np.iterable(xyz):
raise RuntimeError(f'{name} must be a sequence')
self._verts3d = args
self.stale = True

def get_data_3d(self):
Expand Down
2 changes: 1 addition & 1 deletion lib/mpl_toolkits/mplot3d/axis3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _tick_update_position(tick, tickxs, tickys, labelpos):
tick.tick1line.set_linestyle('-')
tick.tick1line.set_marker('')
tick.tick1line.set_data(tickxs, tickys)
tick.gridline.set_data(0, 0)
tick.gridline.set_data([0], [0])


class Axis(maxis.XAxis):
Expand Down
17 changes: 17 additions & 0 deletions lib/mpl_toolkits/mplot3d/tests/test_axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ def test_plot_scalar(fig_test, fig_ref):
ax2.plot(1, 1, "o")


def test_invalid_line_data():
with pytest.raises(RuntimeError, match='x must be'):
art3d.Line3D(0, [], [])
with pytest.raises(RuntimeError, match='y must be'):
art3d.Line3D([], 0, [])
with pytest.raises(RuntimeError, match='z must be'):
art3d.Line3D([], [], 0)

line = art3d.Line3D([], [], [])
with pytest.raises(RuntimeError, match='x must be'):
line.set_data_3d(0, [], [])
with pytest.raises(RuntimeError, match='y must be'):
line.set_data_3d([], 0, [])
with pytest.raises(RuntimeError, match='z must be'):
line.set_data_3d([], [], 0)


@mpl3d_image_comparison(['mixedsubplot.png'])
def test_mixedsubplots():
def f(t):
Expand Down