user2755526 user2755526 - 4 months ago 81
Python Question

How do I add multiple markers to a stripplot in seaborn?

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.

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

Comments