文本标题未出现 matplotlib

text caption not appearing matplotlib

我不明白为什么我的文字标题没有出现在我的情节中。我发现文档对图例和标题标签的放置顺序非常混乱。

我的代码在这里,我不知道哪里错了。一切都如我所料(标题、轴标签、日期格式,...)除了标题的文本根本不在那里。

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')

这是我尝试添加标题的地方:

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')

正在使用 ax.text(0.5,-0.5,txt,transform=ax.transAxes) 您将文本放置在轴坐标中的 (0.5,-0.5) 位置。轴坐标范围从 (0,0)(左下角)到 (1,1) 右上角。 (0.5,-0.5) 因此在坐标轴之外,在这种情况下也在图形之外。

您可以尝试一些介于 0 和 -0.1 之间的数字作为 y 坐标,看看哪个适合您的需要。或者使用图形坐标而不是轴坐标并将文本放置在 y=0, ax.text(0.5,0,txt,transform=fig.transFigure).