Jienkles
Jienkles

Reputation: 133

Get labels for bars in bar plot

I have a dataset with a number of categories and have plotted a bar plot and want to label the bars with texts.

The dataset is reproduced and is car parts and their sales:

Cat           Total Sales  Total units
Exhausts      45000        2333
Tyres         66000        888
Engines       12000        23
Crankshafts   1000         17
Wiperblades   15000        1060
windscreens   2500         1900

I've tried:

import pandas as pd
data = pd.DataFrame(data)
g = sns.barplot(x='Cat', y = 'Total Sales', data=data);

g.set_xticklabels(labels, rotation=90)
plt.title('Sales')

and all sorts of other things, nothing seems to work.

I get the following graph but want to put sales amounts on top of the graphs. Many thanks

enter image description here

Upvotes: 0

Views: 77

Answers (1)

Rajith Thennakoon
Rajith Thennakoon

Reputation: 4130

hope this will helpful

g = sns.barplot(x='Cat',  y = 'Total Sales', data=data);
g.set_xticklabels(g.get_xticklabels(),rotation=90)
for p in g.patches:
    g.annotate(format(p.get_height(), '.2f'), (p.get_x() + p.get_width() / 2., p.get_height()), ha = 'center', va = 'center', xytext = (0, 10), textcoords = 'offset points')

Upvotes: 1

Related Questions