I've been struggling to add a simple thing as value labels to a Pandas dataframe bar plot. I've looked at over 20 threads (with these three being the most helpful - How can I display text over columns in a bar chart in matplotlib?, matplotlib advanced bar plot and Python pandas / matplotlib annotating labels above bar chart columns and nothing is working.
My data is not at all complex. The dataframe structure is:
+------+----------+----------+----------+----------+-------+
| Year | Product1 | Product2 | Product3 | Product4 | Total |
+------+----------+----------+----------+----------+-------+
| 2005 | 123 | 123 | 123 | 123 | 492 |
| 2006 | 111 | 111 | 111 | 111 | 444 |
+------+----------+----------+----------+----------+-------+
with year being the index for the dataframe.
The representation I'm looking for is simple. A stacked bar chart of all the products, with only the value label for 'Total' being displayed at the top of the stacked column (I don't want to represent 'Total' in the chart).
The code that I've currently is:
fig,ax = plt.subplots()
ax =df.ix[:,df.columns.difference(['Total'])].plot.bar(stacked=True, colormap='coolwarm',figsize=(14,12),ax=ax)
ax.set_ylabel("Total sales", fontsize=14)
ax.set_xlabel("Year", fontsize=14)
ax.legend(loc='best', fancybox=True, framealpha=0.5)
for i, label in enumerate(list(df.index)):
score = df.ix[label]['Total']
ax.annotate(str(score), (i - 0.2, score))
fig = ax.get_figure()
fig.savefig('sumplot.png',dpi=100,bbox='Tight')
What I'm getting right now are values that are way off in the sky. I think this is because the height is being determined by the 'Total'+the values of all the other columns? Is there anyway to modify this so that the height is just the height of 'Total'? Fiddling with the value of score in the ax.annotate snippet doesnt help because in there is wide variation in the data values (the data structure above is just representative - not actual data)