Last answered:

17 Jan 2024

Posted on:

15 Nov 2022

0

Resolved: Adding Labels for each Bar in Python

How can I add Labels/Values for each bars in the chart and how to change the color of the labels in python

image.png

2 answers ( 1 marked as helpful)
Instructor
Posted on:

16 Nov 2022

1

Hi Sharfuddin,
thanks for reaching out! You can check out this tutorial in geeksforgeeks where you can see how to add labels to each column in your bar chart:
https://www.geeksforgeeks.org/adding-value-labels-on-a-matplotlib-bar-chart/
To change the colors of labels in matplotlib you can add a color argument, like so:
plt.ylabel("Number of Listings", fontsize = 13, color = 'blue' )
Hope this helps!

Best,
365 Eli

Posted on:

17 Jan 2024

0

Hi Sharfuddin, i found that in the actual matplotlib (3.8.2), you can use plt.bar_label: matplotlib.pyplot.bar_label — Matplotlib 3.8.2 documentation:


# Horizontal bar chart
xy_size = 13
plt.figure(figsize=(9, 6))

bplot = plt.barh(y=df_used_cars['Brand'],
         width=df_used_cars['Cars Listings'],
         color='midnightblue')

plt.title('Used Cars Advertisements by Brand',
          fontdict={'size': xy_size + 2,
                    'weight': 'bold'},
            loc='left')

plt.yticks(rotation=45, fontsize=xy_size)
plt.xlabel('Number of Advertisements', fontsize=xy_size)
plt.bar_label(bplot,
              fontweight='bold',
              color='midnightblue',
              padding=4)
plt.savefig('3_homework_I_barh', bbox_inches='tight')
plt.show()




Submit an answer