diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 3dfd5502778b..8ec650d9eeae 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4199,8 +4199,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 6a4900dc3408..629517406799 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5820,7 +5820,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) @@ -5848,3 +5848,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 745844fb6a1c..243bca3731d6 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -482,3 +482,10 @@ def test_flatiter(): assert 0 == next(it) assert 1 == next(it) + + +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