diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index fc9619de76f2..a6669c648e6b 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4187,8 +4187,9 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None, if (c_none or co is not None or isinstance(c, str) or - (isinstance(c, collections.abc.Iterable) and - isinstance(c[0], str))): + (isinstance(c, collections.Iterable) and + 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_axes.py b/lib/matplotlib/tests/test_axes.py index 08bff9272930..6a3c19053314 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,18 @@ 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(): + # making sure this does not raise an exception + plt.scatter([], []) + plt.scatter([], [], s=[], c=[]) 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