文字标题未出现matplotlib [英] text caption not appearing matplotlib

查看:158
本文介绍了文字标题未出现matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我的文字标题没有出现在我的绘图上.我发现文档中关于图例和标题标签的放置顺序非常混乱.

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

推荐答案

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

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.

对于y坐标,您可以尝试使用介于0到-0.1之间的数字,以查看适合您的需求.或者,使用图形坐标代替轴坐标,并将文本放置在y = 0,ax.text(0.5,0,txt,transform=fig.transFigure).

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

这篇关于文字标题未出现matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆