diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 979d0568a8b8..1f969a3d0ce0 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -514,6 +514,8 @@ def _plot_args(self, tup, kwargs, return_kwargs=False): ncx, ncy = x.shape[1], y.shape[1] if ncx > 1 and ncy > 1 and ncx != ncy: raise ValueError(f"x has {ncx} columns but y has {ncy} columns") + if ncx == 0 or ncy == 0: + return [] label = kwargs.get('label') n_datasets = max(ncx, ncy) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 0a9c30558d12..f30c25fdf17b 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7153,3 +7153,17 @@ def test_artist_sublists(): # Adding to other lists should produce a regular list. assert ax.lines + [1, 2, 3] == [*lines, 1, 2, 3] assert [1, 2, 3] + ax.lines == [1, 2, 3, *lines] + + +def test_empty_line_plots(): + # Incompatible nr columns, plot "nothing" + x = np.ones(10) + y = np.ones((10, 0)) + _, ax = plt.subplots() + line = ax.plot(x, y) + assert len(line) == 0 + + # Ensure plot([],[]) creates line + _, ax = plt.subplots() + line = ax.plot([], []) + assert len(line) == 1