Seaborn: Legend marker in categorical plots

Created on 7 Jun 2016  路  3Comments  路  Source: mwaskom/seaborn

The legend added to categorical plots when passing the hue argument does not reflect shape (or other styles like linewidth) of the marker.

Example:

df1 = pd.DataFrame(dict(A=[2, 1], B=[1, 2]))
df2 = pd.DataFrame(dict(A=[3, 4], B=[3.5, 3]))
df1t = pd.melt(df1)
df2t = pd.melt(df2)
df1t['order'] = ['C', 'D', 'C', 'D']
df2t['order'] = ['C', 'D', 'C', 'D']

sns.swarmplot(x='variable', y='value', hue='order', data=df1t)
sns.swarmplot(x='variable', y='value', hue='order', data=df2t, marker='s')

download

In the previous plot the last two items in the legend should have been squares.

annoyance categorical

Most helpful comment

The same holds for stripplot. My current use case is comparing two categorical datasets with very few points (0,1 per category), so using different markers makes sense.

In seaborn there are two calls to scatter: the first creates the plots, the second is a call with empty data just for creating the legend. A solution for seaborn could be passing the same kws that are passed to the first scatter call, also to the second scatter call (in _CategoricalScatterPlotter.add_legend_data).

Would you consider a PR?

All 3 comments

Technically, different markers aren't part of the swarmplot API, and only "work" because kwargs are passed through to plt.scatter. I agree it would be nice if that worked, though.

(One reason for this is that the swarmplot positioning is driven by radial distances, so once they actually start "swarming" things look pretty bad with square markers).

The same holds for stripplot. My current use case is comparing two categorical datasets with very few points (0,1 per category), so using different markers makes sense.

In seaborn there are two calls to scatter: the first creates the plots, the second is a call with empty data just for creating the legend. A solution for seaborn could be passing the same kws that are passed to the first scatter call, also to the second scatter call (in _CategoricalScatterPlotter.add_legend_data).

Would you consider a PR?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

songololo picture songololo  路  4Comments

stonebig picture stonebig  路  4Comments

ConstantinoSchillebeeckx picture ConstantinoSchillebeeckx  路  4Comments

bondarevts picture bondarevts  路  3Comments

TDaltonC picture TDaltonC  路  3Comments