简体   繁体   English

如何将 matplotlib 中的文本框与图例框对齐?

[英]How to align text box with legend box in matplotlib?

I'm trying to place text at the top left of my figure, and then have the legend next to it at the same vertical height.我试图将文本放在图形的左上角,然后将图例放在它旁边的相同垂直高度。 The following doesn't do what I would expect:以下不符合我的预期:

fig, ax = plt.subplots(figsize=(4, 4))
ax.plot([0, 1,2,3],[2, 5,8,33], label='legend')
plt.legend(loc=[0.5, 1.2], fontsize=20)
fig.text(x=0, y=1.2, s="Text", fontsize=20)

在此处输入图像描述

It seems like the positioning using text() and legend() are inconsistent?使用text()legend()的定位似乎不一致? How can I do this?我怎样才能做到这一点?

Figure.text is in figure co-ordinates, so it is being placed outside the figure. Figure.text在图形坐标中,所以它被放置在图形之外。 plt.legend is in axes co-ordinates. plt.legend在轴坐标中。

fig, ax = plt.subplots(figsize=(4, 4))
ax.plot([0, 1,2,3],[2, 5,8,33], label='legend')
plt.legend(loc=[0.5, 1.2], fontsize=20)
fig.text(x=0, y=1.2, s="Text", fontsize=20, transform=ax.transAxes)

works.作品。 (or plt.text(x=0, y=1.2, s="Text", fontsize=20, transform=ax.transAxes) ) (或plt.text(x=0, y=1.2, s="Text", fontsize=20, transform=ax.transAxes)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM