Python Matplotlib 在框架内放置文本,例如 suptitle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12957801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:16:12  来源:igfitidea点击:

Matplotlib placement of text e.g. suptitle inside the frame

pythonmatplotlibtitle

提问by tarrasch

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

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

enter image description here

在此处输入图片说明

How can i get the suptitles from abovethe frame into the frame?

如何从框架上方获取字幕到框架中?

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.

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

采纳答案by Tobold

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

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

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

Plot showing text relative to axes bounding box.

绘图显示相对于轴边界框的文本。

Hope that helps!

希望有帮助!

回答by dmcdougall

Have you considered axes.title? You can see the documentation for it here. You can also pass x and y coordinates as keyword arguments ax.title("my title", x=0.5, y=0.6).

你考虑过axes.title吗?您可以在此处查看它的文档。您还可以将 x 和 y 坐标作为关键字参数传递ax.title("my title", x=0.5, y=0.6)

Hope this helps.

希望这可以帮助。