如何在seaborn的条纹图中添加多个标记?

2024-10-01 13:23:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道我怎么能在同一个条形图上得到多个标记。在

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

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

也最好我想使相应的'太阳'值作为透明的红色三角形。你知道怎么做到吗?在

谢谢。在

编辑: 所以更好的方法是声明一个myax=plt.轴() 把我的斧头传给每一个条纹图(ax=我的斧头)。我认为这是应该这样做的。在


Tags: lambda标记satmarkercolorsunapplysns
1条回答
网友
1楼 · 发布于 2024-10-01 13:23:51

小心,这有点老套,但你可以:

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

相关问题 更多 >