'Seaborn plotting annotations in bar charts horizontal

I have a dataframe like so:

switch_summary                             duration   count
McDonalds -> Arbys -> McDonalds            0.067      1
Wendys -> Popeyes -> McDonalds -> KFC      0.293      1
Arbys -> Wendys -> Popeyes -> McDonalds    0.542      2
Arbys -> McDonalds -> KFC                  1.075      1
KFC -> Arbys -> Wendys -> Popeyes          2.123      3
KFC -> Wendys -> Popeyes -> Arbys          2.297      1

I want to create a seaborn plot to visualize both the duration and the count.

I have the following code:

plt.figure(figsize = [15,7])
ax = (sns.barplot(x = 'duration',
                  y = 'switch_summary',
                  palette = sns.color_palette('winter', 10),
                  data = df))

for p in ax.patches:
    width = p.get_width()
    plt.text( 0.1 + p.get_width(), p.get_y() + 0.55 * p.get_height(),
            '~{:1.2f}\n days'.format(width),
            ha = 'center', va = 'center', color = 'black', weight = 'bold')

ax = ax.set(title = 'Top 10 Fastest Trends',
            xlabel = 'Total Duration in Trend',
            ylabel = 'Restaurant Trend')

This code will display the duration but I also want to display the count.

How can I display the count in the plt.text() portion of the code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source