title: Displaying totals above stacked bars in barchart: matplotlib.pyplot
body: <p>I am just starting out with matplotlib.pyplot and am a little stuck. </p>
  
  <p>Using the example in the matpltlib.pyplot documentation, I have created a stacked bar chart <img src="https://i.stack.imgur.com/sEs4V.png" alt="enter image description here"> using the following code:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  N = 7
  OECD = (242, 244, 255, 263, 269, 276, 285)
  NonOECD = (282, 328, 375, 417, 460, 501, 535)
  Sum = ('524', '572', '630', '680', '729', '777', '820')
  ind = np.arange(N)
  width = 0.5
  
  p1 = plt.bar(ind, NonOECD, width, color = 'r')
  p2 = plt.bar(ind, OECD, width, color = 'b', bottom = NonOECD)
  
  plt.ylabel('Quadrillion Btu')
  plt.title('World Total Energy Consumption 2010 - 2040')
  plt.xticks(ind+width/2., ('2010', '2015', '2020', '2025', '2030', '2035', '2040'))
  plt.yticks(np.arange(0, 1001, 200))
  plt.legend((p1[0], p2[0]), ('Non - OECD', 'OECD'), loc = 2, frameon = 'false')
  plt.tick_params(top = 'off', bottom = 'off', right = 'off')
  plt.grid(axis = 'y', linestyle = '-')
  
  plt.show()
  </code></pre>
  
  <p>However I want to display the totals on top of the bars and I cannot quite work out how. I have seen <a href="https://stackoverflow.com/questions/7423445/how-can-i-display-text-over-columns-in-a-bar-chart-in-matplotlib?rq=1">this post</a> but am having issues:</p>
  
  <pre><code>for ii,rect in enumerate(p1):
      h1 = rect.get_height()
  for ii,rect in enumerate(p2):
      h2 = rect.get_height()
      height = 
      plt.text(rect.get_x()+rect.get_width()/2., height, '%s'% (Sum[ii]), ha = 'center', va='bottom')
  </code></pre>
  
  <p>If I use <code>height = h1</code> I get <img src="https://i.stack.imgur.com/HnELq.png" alt="enter image description here">; if I use <code>height = h2</code> I get <img src="https://i.stack.imgur.com/AL6oo.png" alt="enter image description here">; if I use <code>height = h1 + h2</code> I get <img src="https://i.stack.imgur.com/g5V94.png" alt="enter image description here">. </p>
  
  <p>What I want is these numbers sitting directly on above the second (blue) bar [like the 524 on the 2010 bar in my 1st attempt]. Am I missing something really obvious?</p>
  
  <p>As always, any help would be much appreciated!
  Cheers</p>