PixelPusher
PixelPusher

Reputation: 126

Seaborn Countplot: Displaying the counts on bar

I have written the following code:

plt.figure(figsize=(12,6)) 
ax = sns.countplot(x='Region', data=data, order=data['Region'].value_counts().iloc[:15].index)
plt.xticks(rotation='vertical')
plt.xlabel('Regions', fontsize=14)
plt.ylabel('Total Restaurants', fontsize=14)
plt.title('Total Restaurnats present in Each Regions', fontsize=20, y=1.03, weight='bold')

My output:

enter image description here

Counting values

values = list(data['Region'].value_counts()[:15].values)
values

My output:

[203, 192, 177, 124, 113, 112, 99, 99, 94, 86, 82, 68, 67, 55, 53]

I want to show the respective values on each bar, like 203 on the first bar; 192 on the second and so on.
Is there any way to do this? I would be more than happy for your help.

Thanks in advance!

Upvotes: 0

Views: 3318

Answers (1)

pyano
pyano

Reputation: 1978

This is a version for vertical bars:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


N=11
t = np.linspace(0,0.5*np.pi,N)                                     #---- generate data ----
df = pd.DataFrame({'A':10*np.sin(t),
                   'B':10*np.cos(t)} )

fig = plt.figure(figsize=(15,8))                                   #---- set up graphics ----
ax1 = fig.add_subplot(111)

width=0.9                                                          #---- tuning parameters for the bar plot ----
dy = 0.2

df['A'].plot(kind='bar',width=width, color='b', alpha=0.3)         #---- define the bar plot ----
for x,y in enumerate(df['A']):                                     #---- annotate the bars ----
    plt.annotate(str(np.around(y,decimals=3)), xy=(x+width/2, y+dy), va='center',ha='right', color='b', fontweight='bold', fontsize=16)
    
df['B'].plot(kind='bar',width=width, color='r', alpha=0.2)         #---- define the bar plot ----
for x,y in enumerate(df['B']):                                     #---- annotate the bars ----
    plt.annotate(str(np.around(y,decimals=3)), xy=(x+width/2, y+dy), va='center',ha='right', color='r', fontweight='bold', fontsize=16)

plt.show()
pic_name='psc_annotaed_vertical_bars.png'
fig.savefig(pic_name, transparency=True)

enter image description here

And this is a version vor horizontal bars:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

N=11
t = np.linspace(0,0.5*np.pi,N)
df = pd.DataFrame({'A':10*np.sin(t),'B':10*np.cos(t)} )

fig = plt.figure(figsize=(15,8))
ax1 = fig.add_subplot(111)

df['A'].plot(kind='barh',width=0.9, color='b', alpha=0.3) 
for y, x in enumerate(df['A']): 
    plt.annotate(str(np.around(x,decimals=3)), xy=(x-0.01, y), va='center',ha='right', color='b', fontweight='bold', fontsize=16)

df['B'].plot(kind='barh',width=0.9, color='r', alpha=0.2) 
for y, x in enumerate(df['B']): 
    plt.annotate(str(np.around(x,decimals=3)), xy=(x-0.01, y), va='center',ha='right', color='r', fontweight='bold', fontsize=16)

plt.show()
pic_name='psc_annotaed_horizontal_bars.png'
fig.savefig(pic_name, transparency=True)

enter image description here

Upvotes: 1

Related Questions