Add a panel to bar chart in matplotlib
<p>How can I add a panel containing (improvement percentage for each algorithm) to this bar chart? It should, for example, be located in top right-corner of the chart. I want to do this in order to be easier for the reader to see how much improvement each algorithm has compared to its non-greedy version.</p>

<p>The panel should contain this:</p>

<pre><code>UB-improvement= (("UB+Greedy"- "UB")/"UB")*100
IB-improvement=(("IB+Greedy"- "IB")/"IB")*100
SVD-improvement=(("SVD+Greedy"- "SVD")/"SVD")*100
TOP_N-improvement=(("TOP_N+Greedy"- "TOP_N")/"TOP_N")*100
</code></pre>

<p><a href="http://i.stack.imgur.com/qCS9R.png" rel="nofollow"><img src="http://i.stack.imgur.com/qCS9R.png" alt="enter image description here"></a></p>

<p>Here is my code:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt
N = 1
ind = np.arange(N)  # the x locations for the groups
width = 0.2     # the width of the bars

fig = plt.figure()
ax = plt.subplot(111)
ub = 5444
rects1 = ax.bar(ind, ub, width, color='red')
ub_greedy = 6573
rects2 = ax.bar(ind+width, ub_greedy, width, color='darkviolet')

ib = 1521
rects3 = ax.bar(ind+2*width, ib, width, color='black')
ib_greedy = 5483
rects4 = ax.bar(ind+3*width, ib_greedy, width, color='blue')

svd=553
rects5 = ax.bar(ind+4*width, svd, width, color='grey')
svd_greedy=1225
rects6 = ax.bar(ind+width*5, svd_greedy, width, color='gold')

pop=7
rects7 = ax.bar(ind+width*6, pop, width, color='brown')
pop_greedy=53
rects8 = ax.bar(ind+width*7, pop_greedy, width, color='lime')

ax.set_ylabel('Owner utilities')
ax.set_xticklabels('')

ax.set_xticks(ind+width)
ax.legend((rects1[0], rects2[0], rects3[0],rects4[0],rects5[0], rects6[0], rects7[0],rects8[0]), ('UB','UB+Greedy','IB','IB+Greedy','SVD','SVD+Greedy','TOP_N','TOP_N+Greedy') ,loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=8)

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/1.9, 1.01*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)
autolabel(rects6)
autolabel(rects7)
autolabel(rects8)

plt.title("Total owner utilities for different algorithms",y=1.08)
plt.show()
</code></pre>

<p>add the following code just before <code>autolabel(rects1)</code> line. (I use python 3)</p>

<p>I used <a href="http://stackoverflow.com/questions/7045729/automatically-position-text-box-in-matplotlib">automatically position text box in matplotlib</a></p>

<pre><code>from matplotlib.offsetbox import AnchoredText
def func(greedy, non_greedy): return (greedy - non_greedy)/non_greedy*100

anchored_text = AnchoredText('UB-improvement={:.4}%\n'.format(func(ub_greedy, ub))+
'IB-improvement={:.4}%\n'.format(func(ib_greedy, ib))+
'SVD-improvement={:.4}%\n'.format(func(svd_greedy, svd))+
'TOP_N-improvement={:.4}%\n'.format(func(pop_greedy, pop)), loc=1)
ax.add_artist(anchored_text)
</code></pre>

python matplotlib bar-chart