show percentage in seaborn countplot site 3astackoverflow com

Solutions on MaxInterview for show percentage in seaborn countplot site 3astackoverflow com by the best coders in the world

showing results for - "show percentage in seaborn countplot site 3astackoverflow com"
Luisa
31 Apr 2020
1def with_hue(plot, feature, Number_of_categories, hue_categories):
2    a = [p.get_height() for p in plot.patches]
3    patch = [p for p in plot.patches]
4    for i in range(Number_of_categories):
5        total = feature.value_counts().values[i]
6        for j in range(hue_categories):
7            percentage = '{:.1f}%'.format(100 * a[(j*Number_of_categories + i)]/total)
8            x = patch[(j*Number_of_categories + i)].get_x() + patch[(j*Number_of_categories + i)].get_width() / 2 - 0.15
9            y = patch[(j*Number_of_categories + i)].get_y() + patch[(j*Number_of_categories + i)].get_height() 
10            ax.annotate(percentage, (x, y), size = 12)
11    plt.show()
12
13def without_hue(plot, feature):
14    total = len(feature)
15    for p in ax.patches:
16        percentage = '{:.1f}%'.format(100 * p.get_height()/total)
17        x = p.get_x() + p.get_width() / 2 - 0.05
18        y = p.get_y() + p.get_height()
19        ax.annotate(percentage, (x, y), size = 12)
20    plt.show()