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.