0

I am new to generating bar graphs with python. I have managed to write the following code to generate a bar graph. The graph generated by the code is attached herewith the post.

method = ['Fx', 'Lx']        
times = ['pre process', 'prop calc', 'moc calc', 'total'] 
pre_processing = [36, 36]
prop_calc = [45, (9.0/60.0)]
moc_calc = [73, (109-(36+9.0/60.0))]
total = [154, 109]
null = [0,0]

n_groups = 2

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.2
opacity = 0.8

rects1 = plt.bar(index, pre_processing, bar_width,
                 alpha=opacity,
                 color='c',
                 label='pre_processing')

rects2 = plt.bar(index + bar_width, prop_calc, bar_width,
                 alpha=opacity,
                 color='r',
                 label='prop_calc')

rects3 = plt.bar(index + 2*bar_width, moc_calc, bar_width,
                 alpha=opacity,
                 color='g',
                 label=times[2])            

rects4 = plt.bar(index + 3*bar_width, total, bar_width,
                 alpha=opacity,
                 color='b',
                 label=times[3])    

rects5 = plt.bar(index+4*bar_width,null,bar_width,
                 alpha=opacity,
                 color='w') 

plt.ylabel('Time [min]')
plt.xticks(index + bar_width, method) 

fontP = FontProperties()
fontP.set_size('medium')

plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.00),
          fancybox=True, ncol=3, prop=fontP)  

fig.savefig("Time comparison", format='eps', dpi=1200)

plt.tight_layout()
plt.show()

bar chart generated

Can somebody please tell me how can I print the individual value of each y variable on top of each bar?

Aspro
  • 45
  • 2
  • 7
  • 1
    There are many [examples](https://stackoverflow.com/q/40489821/8881141) for this question on SO and even matplotlib has one sample chart: https://matplotlib.org/examples/api/barchart_demo.html – Mr. T Jun 19 '18 at 14:02
  • 1
    Thank you for referring. @Mr.T . That solved my problem. – Aspro Jun 19 '18 at 14:21

0 Answers0