如何在seaborn的stripplot中添加多个标记?

时间:2016-07-29 05:08:20

标签: python pandas matplotlib seaborn

我想知道如何在同一条带图中获得多个标记。

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")

这似乎不起作用,因为标记只接受单个值。

同样最好我想制作相应的' Sun'值为透明红色三角形。知道如何实现这一目标吗?

谢谢。

编辑: 所以更好的方法是声明一个my_ax = plt.axes() 并将my_ax传递给每个stripplot(ax = my_ax)。我相信这是应该做的。

2 个答案:

答案 0 :(得分:3)

小心它有点hacky但是你走了:

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

答案 1 :(得分:0)

Lmplot是您的朋友!但是,不可能添加倍数:/ [或者至少我还没有弄清楚]

enter image description here