How to put a variable in x/y label using matplotlib module pyplot

Atul Kakrana picture Atul Kakrana · May 3, 2012 · Viewed 29.7k times · Source

I am generating a stacked bar graph in Python3.2 and using 'Matplotlib' module 'pyplot'. The only problem is that in the 'xlabel' I have to include a variable (integer) for number of entries analyzed. I have tried a lot but could not find the way of including the variable in xlabel.

Part of my code:

plt.ylabel('Percentage', fontproperties=font_manager.FontProperties(size=10))
plt.xlabel('position', fontproperties=font_manager.FontProperties(size=10))
plt.title("'Graph-A",fontproperties=font_manager.FontProperties(size=10))

plt.xticks(np.arange(22), np.arange(1,23), fontproperties=font_manager.FontProperties(size=8))
plt.yticks(np.arange(0,121,5), fontproperties=font_manager.FontProperties(size=8))
plt.legend((p1[0], p2[0], p3[0], p4[0],p5[0]), ('A','B','C','D','E'), loc=1, prop=font_manager.FontProperties(size=7))

The variable 'entries' hold the value of entries processed and I need to include this value in xlabel. Please help?

AK

Answer

Harpal picture Harpal · May 4, 2012

If I got you right, you could try:

plt.xlabel('position %d' % entries, fontproperties=font_manager.FontProperties(size=10))