0

I'm having some difficulty finding an easy way to label a bar graph with values above the bar, as I have multiple groups of bars. The code is written in Python 3.8 and attached below.

import numpy as np
df_Data = df_data.sort_values(by = 'Very interested', ascending = False)
df_per = ((df_Data.iloc[0:len(df_Data),1:4]/2233)*100).round(2)

names = ["Data Analysis / Statistics", "Machine Learning", "Data Visualization", "Big Data (Spark / Hadoop)", "Deep Learning", "Data Journalism "]

bar_chart = df_per.plot(kind = 'bar', figsize = (20,8), width = 0.8, color = ['#5cb85c','#5bc0de','#d9534f'])
plot.legend(frameon = False)

position = []
list = []
empty = []

for x in range(0,len(df_per)):
    position.append(x)
    yval = np.array(position)
    
plot.xticks(yval, names)

bar_label = np.arange(0,100,10)

for x in range(0,len(df_per)):
    values_array = df_per.iloc[x].to_numpy()
    list.append(values_array)
    
for position in range(0,len(df_per)):
    positional_values = np.array([position-0.3,position-0.1, position+0.2])
    empty.append(positional_values)

for index in range(0,3):
    for group in range(0,len(empty)):
        bar_chart.text(empty[group][index], list[group][index], list[group][index])
        
for spine in bar_chart.spines:
    bar_chart.spines[spine].set_visible(False)

The produced graph:

Graph produced from listed code

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Please explain problem with your code. Actually, what is your question? Image appears to have numbers over bars per your title. – Parfait Sep 12 '20 at 19:08
  • @Parfait Mostly the chart was okay, but the numbers for the green bars weren't centered and all the numbers were sitting just inside the bar. – Trenton McKinney Sep 12 '20 at 21:15

0 Answers0