8

I cannot understand why my text caption does not appear on my plot. I am finding the documentation pretty confusing about the order of placement for the legend and caption labels..

My code is here, I don't know what is wrong. Everything is appearing as I would expect (title, axes labels, date format, ...) except the text for the caption is not on there at all.

fig = plt.figure(figsize=(24, 12), dpi=60)
ax = fig.add_subplot(111)
plt.plot(datetime_series,ws_cumsum_mean1,label='1979-1994')
plt.plot(datetime_series,ws_cumsum_mean2,label='1996-2005')
plt.plot(datetime_series,ws_cumsum_mean3,label='2006-2014')
txt = '''Caption text'''
plt.legend(loc='best')

this is where I try to add the caption:

ax.text(0.5,-0.5,txt,transform=ax.transAxes)

.

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m'))
plt.ylabel('Y label title')
plt.xlabel('X label title')
plt.title('Plot title')
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
pwprnt
  • 521
  • 3
  • 9
  • 27

1 Answers1

18

Using ax.text(0.5,-0.5,txt,transform=ax.transAxes) you are placing the text at position (0.5,-0.5) in axes coordinates. Axes coordinates range from (0,0) (lower left corner) to (1,1) upper right corner. (0.5,-0.5) is thus outside the axes and in this case also outside the figure.enter image description here

You may try some numbers between 0 and -0.1 for the y coordinate to see what fits your needs. Alternatively use figure coordinates instead of axes coordinates and place the text at y=0, ax.text(0.5,0,txt,transform=fig.transFigure).

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thans, but yes, I put (0.5,-0.5) so that it should my text does not appear in the figure, and that it would appear below.. I just want a figure caption below the figure. However, with my current code as listed above in the question, no text appears at all. – pwprnt May 30 '17 at 22:40
  • Below the figure means outside the figure. Everything that is outside is not visible. Compare it to your Browser window; if you place the browser window below the screen (let's say at y=-500 pixels) you cannot see it anymore. – ImportanceOfBeingErnest May 30 '17 at 22:44