UserWarning: Patch object at has a label of '_K' which cannot be automatically added to the legend

Viewed 1057

Plotting using seaborn pairplot, has worked previously with different columns as hue but when plotting hue='letters' column, I am getting the user warning and it is unable to plot the legend? I am not sure why.

Example of column 'letters'

letters
    _J
    _A
    _H
    _H
    _L

    
fig = sns.pairplot(x_vars=x, y_vars=y, data=df, hue="letters", size=5, aspect=2.65)
    
figlegend = self.fig.legend(handles, labels, **kwargs)
/home/ec2-user/anaconda3/envs/chainer_p36/lib/python3.6/site-packages/seaborn/axisgrid.py:98: UserWarning: The handle <matplotlib.patches.Patch object at xxxxx has a label of '_H' which cannot be automatically added to the legend.

Same error for all _letters in column.

1 Answers

This is due to the fact that matplotlib developers have chosen to ignore labels starting with _.

From the legend guide:

Those artists with an empty string as label or with a label starting with "_" will be ignored.

You should create a new column where you strip the underscore to use as hue if you want the legend.

Related