Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show values above the bar Python

I have a very simple DataFrame, and I want to apply a goupby so I can plot the common count between the two columns.

Then I want to plot the grouped DataFrame.

I did it by the following lines of codes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my_data = {
    "source": ['Live','Twitter','Twitter','Telegram','Telegram'],
    "second_source":['Qa','Unspecified','Da','Hzo','Tolib'],
    "count":[7,1,1,1,1]
}
my_dataframe = pd.DataFrame(my_data)

# Make the dataframe goupedby for the first two columns
# Then plot the count for them both
grouped_dataframe = my_dataframe.groupby(['source', 'second_source']).agg('sum')

grouped_dataframe.plot(kind='bar', figsize=(10,5))
plt.xticks(rotation=40, ha='right')
plt.title("Sources")
plt.show()

Output:

Output Image

How can I show the count numbers above every bar in the plot?

>Solution :

Modify your code to:

ax = grouped_dataframe.plot(kind='bar', figsize=(10,5))

for x, val in enumerate(grouped_dataframe['count']):
    ax.text(x, val, val, va='bottom', ha='center')

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading