简体   繁体   English

如何使用Seaborn为小提琴图添加标签

[英]How to add labels to a violin plot using Seaborn

I'm trying to label a violin plot with Seaborn using: 我试图用Seaborn标记一个小提琴情节:

ax = sns.violinplot(x='distance', y='Length', data=class_A, scale= 'count')

sns.violinplot without hue set 没有色调的sns.violinplot

When I add hue='population' , which is what I'd like to label each violin plot with, I lose my KDE and it only shows the boxplot. 当我添加hue='population' ,我想用每个小提琴情节标注,我丢失了我的KDE,它只显示了boxplot。

With Hue 有顺化

Any idea why this is happening? 知道为什么会这样吗? Any suggestions to label each violinplot by a column? 有没有建议按列标记每个小提琴图?

I believe your data has a one-to-one mapping from distance to population . 我相信您的数据具有从distancepopulation的一对一映射。 When you add in hue , seaborn is trying to make a violin plot for each distance for each population . 当您添加hueseaborn正在尝试为每个population制作每个距离的小提琴图。 This would be about 400 violin plots based on your data. 根据您的数据,这将是大约400个小提琴图。 The problem is that only 20 of these combinations have any data because of the one to one mapping. 问题是由于一对一的映射,这些组合中只有20个具有任何数据。 Thus, there is no point in using hue . 因此,使用hue没有意义。

You can however, change the x labels in the plot to show both the distance and the population with something like this. 但是,您可以更改绘图中的x标签,以显示距离和人口,如下所示。

df_labels = class_A[['distance', 'population']].sort_values('distance').drop_duplicates()
new_labels = df_labels.distance + ' \n' + df_labels.population
ax.set_xticklabels(new_labels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM