From 8924ab8f84a5cd964a7a78d64c93775d19dcf9c0 Mon Sep 17 00:00:00 2001 From: David Matos Date: Sun, 27 Jun 2021 17:23:48 +0200 Subject: [PATCH 1/2] Plot nothing for incompatible 0 shape in x,y data --- lib/matplotlib/axes/_base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 979d0568a8b8..c72bf9773248 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 not ncx or not ncy: + return [] label = kwargs.get('label') n_datasets = max(ncx, ncy) From cf4c2a8bf0bb667c6d74c0f6950743a1e8cab912 Mon Sep 17 00:00:00 2001 From: David Matos Date: Tue, 29 Jun 2021 17:24:36 +0200 Subject: [PATCH 2/2] Add test for incompatible 0 column in plots --- lib/matplotlib/axes/_base.py | 2 +- lib/matplotlib/tests/test_axes.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index c72bf9773248..1f969a3d0ce0 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -514,7 +514,7 @@ 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 not ncx or not ncy: + if ncx == 0 or ncy == 0: return [] label = kwargs.get('label') 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