Reputation: 846
Is it possible to create a bar diagram in matplotlib which shows the percentage deviation with a vertical error bar like in the picture?
Upvotes: 4
Views: 201
Reputation: 30920
You can do something like this:
you can place the start point of the error bar using a tuple and calculate the percentage of the difference between both values and enter them in the error field:
import numpy as np
from matplotlib import pyplot
val = [30,20]
dif = [(0,0),[0,(val[0]-val[1])]]
pval = (val[0]-val[1])/val[0]
ind = np.arange(len(val))
width = 1
colours = ['red','blue']
pyplot.figure()
pyplot.title('Error Rate')
pyplot.bar(ind, val, width, color=colours, align='center', yerr=dif,
ecolor='k')
pyplot.ylabel('Age (years)')
pyplot.xticks(ind,('Young Male','Young Female'))
height = pval
pyplot.text(ind[1], val[0]-1, '{:.1%}'.format(pval), ha='right', va='bottom')
pyplot.show()
Upvotes: 1