Matplotlib 放置文本,例如框架内的字幕 [英] Matplotlib placement of text e.g. suptitle inside the frame

查看:30
本文介绍了Matplotlib 放置文本,例如框架内的字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已将字幕放在框架上方,如下所示:

So far i have placed my suptitles above the frame, like this:

如何将上方框架的字幕放入框架中?

到目前为止,我有一个解决方案,它只打印文本并通过计算xlim和ylim将其设置在正确的位置.然而,这是容易出错的,如果文本不同,它看起来很糟糕.有没有一种方法可以将suplabel设置到框架中?或者只是将文本放在框架下方并居中?如果我不需要知道框架内显示的数据,那将非常方便.

So far i have a solution that just prints a text and sets it on the right position with computing xlim and ylim. However this is errorprone and if the text is different it just looks aweful. Is there a way to set the suplabel into the frame? Or just place text below the frame and centered? it would be really convenient, if i did not need to know about the data that is displayed inside the frame.

推荐答案

您使用 text 的解决方案也是我的首选解决方案.但是,您不需要根据 xlim 和 ylim 计算位置.如果设置 transform = ax.transAxes ,则定位文本的坐标将视为相对于轴边界框(0,0为左下角).像这样:

Your solution using text is also my go-to solution. However, you don't need to compute the position based on xlim and ylim. If you set transform=ax.transAxes the coordinates for positioning the text are taken as being relative to the axes bounding box (0,0 being the lower left corner). Like so:

data = range(1,10);
fig = figure()
for i in range(6):
    ax = fig.add_subplot(2,3,i)

    ax.text(.5,.9,'centered title',
        horizontalalignment='center',
        transform=ax.transAxes)

    ax.plot(data)
show()

希望有帮助!

这篇关于Matplotlib 放置文本,例如框架内的字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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