Skip to content

_axes.py.scatter() array index out of bound / calling from seaborn #12641

@esvhd

Description

@esvhd

Bug report

Bug summary

Seeing this with matplotlib 3.0.1 and seaborn 0.9. cc @mwaskom

matplotlib.axes._axes.scatter() tests parameter c hits array index out of bound when c is an empty array.

I encountered this while using seaborn.swarmplot() with hue= parameter. Essentially this plots each group (hue, in addition to x-axis category) and some times a group can be empty for a major x-axis category.

Suggest potential fix to test length of c as well before accessing c[0].

        if (c_none or
                co is not None or
                isinstance(c, str) or
                (isinstance(c, collections.Iterable) and 
                    len(c) > 0 and # proposed fix here
                    isinstance(c[0], str))):

Code for reproduction

import pandas as pd
import seaborn as sns

df = pd.DataFrame(dict(category=['a', 'a', 'b', 'b', 'c'],
                       hue=['x', 'y', 'x', 'y', 'x'],
                       values=[1, 2, 3, 4, 3]))
sns.swarmplot('category', 'values', hue='hue', data=df, dodge=True)

Actual outcome

This is the error I see:

IndexError                                Traceback (most recent call last)
<ipython-input-5-0368a082f6a8> in <module>
----> 1 sns.swarmplot('category', 'values', hue='hue', data=df, dodge=True)

h:\anaconda3\lib\site-packages\seaborn\categorical.py in swarmplot(x, y, hue, data, order, hue_order, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)
   2989                        linewidth=linewidth))
   2990 
-> 2991     plotter.plot(ax, kwargs)
   2992     return ax
   2993 

h:\anaconda3\lib\site-packages\seaborn\categorical.py in plot(self, ax, kws)
   1444     def plot(self, ax, kws):
   1445         """Make the full plot."""
-> 1446         self.draw_swarmplot(ax, kws)
   1447         self.add_legend_data(ax)
   1448         self.annotate_axes(ax)

h:\anaconda3\lib\site-packages\seaborn\categorical.py in draw_swarmplot(self, ax, kws)
   1429                     kws.update(c=point_colors)
   1430                     if self.orient == "v":
-> 1431                         points = ax.scatter(cat_pos, swarm_data, s=s, **kws)
   1432                     else:
   1433                         points = ax.scatter(swarm_data, cat_pos, s=s, **kws)

h:\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1803                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1804                         RuntimeWarning, stacklevel=2)
-> 1805             return func(ax, *args, **kwargs)
   1806 
   1807         inner.__doc__ = _add_data_doc(inner.__doc__,

h:\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4194                 (isinstance(c, collections.Iterable) and
   4195                     # len(c) > 0 and
-> 4196                     isinstance(c[0], str))):
   4197             c_array = None
   4198         else:

IndexError: index 0 is out of bounds for axis 0 with size 0

Happy to create a PR if we have happy with the proposed length test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions