简体   繁体   中英

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? How can I do this?

Figure.text is in figure co-ordinates, so it is being placed outside the figure. plt.legend is in axes co-ordinates.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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