How do I add multiple markers to a stripplot?

9.9k Views Asked by At

I would like to know how I could get multiple markers in the same strip plot.

tips = sns.load_dataset("tips")

coldict={'Sun':'red','Thur':'blue','Sat':'yellow','Fri':'green'}
markdict={'Sun':'x','Thur':'o','Sat':'o','Fri':'o'}

tips['color']=tips.day.apply(lambda x: coldict[x])
tips['marker']=tips.day.apply(lambda x: markdict[x])

m=sns.stripplot('size','total_bill',hue='color',\
                marker='marker',data=tips, jitter=0.1, palette="Set1",\
                split=True,linewidth=2,edgecolor="gray")

This doesn't seem to work as marker only accepts a single value.

Also preferably I would like to make the corresponding 'Sun' values as transparent red triangles. Any idea how this could be achieved?

Thank you.

Edit: So a much better way to do it was to declare a my_ax = plt.axes() and pass my_ax to each stripplot(ax=my_ax). I believe this is the way it should be done.

3

There are 3 best solutions below

3
On BEST ANSWER

Caution it's a little hacky but here ya go:

import sns

tips = sns.load_dataset("tips")

plt.clf()
thu_fri_sat = tips[(tips['day']=='Thur') | (tips['day']=='Fri') | (tips['day']=='Sat')]
colors = ['blue','yellow','green','red']
m = sns.stripplot('size','total_bill',hue='day',
                  marker='o',data=thu_fri_sat, jitter=0.1, 
                  palette=sns.xkcd_palette(colors),
                  split=True,linewidth=2,edgecolor="gray")

sun = tips[tips['day']=='Sun']
n = sns.stripplot('size','total_bill',color='red',hue='day',alpha='0.5',
                  marker='^',data=sun, jitter=0.1, 
                  split=True,linewidth=0)
handles, labels = n.get_legend_handles_labels()
n.legend(handles[:4], labels[:4])
plt.savefig('/path/to/yourfile.png')

enter image description here

0
On

Lmplot is your friend! However, not possible to add multiples :/ [or at least I did not yet figure it out]

enter image description here

0
On

Starting version 0.12, you can achieve this with the seaborn object interface:

import seaborn.objects as so

tips = sns.load_dataset("tips")
coldict={'Sun':'red','Thur':'blue','Sat':'yellow','Fri':'green'}
markdict={'Sun':'v','Thur':'o','Sat':'o','Fri':'o'}

(
    so.Plot(tips, x="size", y="total_bill", color="day", marker="day")
    .add(
        so.Dot(pointsize=7, edgecolor="gray"),
        # so.Dodge(), # use this if you want to separate the markers with different colors
        so.Jitter(0.5)
    )
    .scale(
        color=so.Nominal(coldict),
        marker=so.Nominal(markdict)
    )
)

Without Dodge: Stripplot without Dodge

With Dodge: Stripplot with Dodge