From ce5533c9e5d535d7ce054ecb9f1e3e415c96fde3 Mon Sep 17 00:00:00 2001 From: esvhd Date: Mon, 29 Oct 2018 20:30:36 -0400 Subject: [PATCH 1/3] Fix index out of bound error for testing first element of iterable. --- lib/matplotlib/axes/_axes.py | 3 ++- lib/matplotlib/tests/test_cbook.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index adbf440cb131..e8d15559a739 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4188,7 +4188,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None, co is not None or isinstance(c, str) or (isinstance(c, collections.Iterable) and - isinstance(c[0], str))): + len(c) > 0 and + isinstance(cbook.safe_first_element(c), str))): c_array = None else: try: # First, does 'c' look suitable for value-mapping? diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 988fb2d83f85..69b887020d91 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -526,3 +526,10 @@ def test_contiguous_regions(): assert cbook.contiguous_regions([False]*5) == [] # Empty mask assert cbook.contiguous_regions([]) == [] + + +def test_safe_first_element_pandas_series(pd): + # delibrately create a pandas series with index not starting from 0 + s = pd.Series(range(5), index=range(10, 15)) + actual = cbook.safe_first_element(s) + assert actual == 0 From af90f4f7e3000515cc4f11186b9338fa1e11f9e0 Mon Sep 17 00:00:00 2001 From: esvhd <7330385+esvhd@users.noreply.github.com> Date: Wed, 31 Oct 2018 20:42:40 -0400 Subject: [PATCH 2/3] Added test cases for scatter plot: 1) empty data/color, 2) pandas.Series with non-0 starting index. --- lib/matplotlib/tests/test_axes.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 08bff9272930..d6f893602276 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5828,7 +5828,7 @@ def test_spines_properbbox_after_zoom(): bb = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer()) # this is what zoom calls: ax._set_view_from_bbox((320, 320, 500, 500), 'in', - None, False, False) + None, False, False) bb2 = ax.spines['bottom'].get_window_extent(fig.canvas.get_renderer()) np.testing.assert_allclose(bb.get_points(), bb2.get_points(), rtol=1e-6) @@ -5856,3 +5856,17 @@ def test_gettightbbox_ignoreNaN(): t = ax.text(np.NaN, 1, 'Boo') renderer = fig.canvas.get_renderer() np.testing.assert_allclose(ax.get_tightbbox(renderer).width, 532.444444) + + +def test_scatter_series_non_zero_index(pd): + # create non-zero index + ids = range(10, 18) + x = pd.Series(np.random.uniform(size=8), index=ids) + y = pd.Series(np.random.uniform(size=8), index=ids) + c = pd.Series([1, 1, 1, 1, 1, 0, 0, 0], index=ids) + plt.scatter(x, y, c) + + +def test_scatter_empty_data(): + plt.scatter([], []) + plt.scatter([], [], s=[], c=[]) From e2ca262380fcac2cc59544c5d764ebd859077283 Mon Sep 17 00:00:00 2001 From: esvhd <7330385+esvhd@users.noreply.github.com> Date: Thu, 1 Nov 2018 19:44:18 -0400 Subject: [PATCH 3/3] Added comment for test. --- lib/matplotlib/tests/test_axes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index d6f893602276..6a3c19053314 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5868,5 +5868,6 @@ def test_scatter_series_non_zero_index(pd): def test_scatter_empty_data(): + # making sure this does not raise an exception plt.scatter([], []) plt.scatter([], [], s=[], c=[])