简体   繁体   中英

How to add labels to a violin plot using Seaborn

I'm trying to label a violin plot with Seaborn using:

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

sns.violinplot without hue set

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.

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 . When you add in hue , seaborn is trying to make a violin plot for each distance for each population . This would be about 400 violin plots based on your data. The problem is that only 20 of these combinations have any data because of the one to one mapping. Thus, there is no point in using hue .

You can however, change the x labels in the plot to show both the distance and the population with something like this.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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