plt画图像图例的位置怎么写代码,如何在图坐标中的matplotlib中指定图例位置

I am aware of the bbox_to_anchor keyword and this thread, which very helpfully suggests how to manually place the legend:

However, I'd like to use the coordinates of my x- and y-axis in the graph to specify the legend position (inside the plot), as I might need to move the figure into a large figure with a different axis environment, and I don't want to manually play around with those coordinates every time I do this. Is this possible?

Edit: A small example is here:

import numpy as n

f, axarr = plt.subplots(2,sharex=True)

axarr[1].set_ylim([0.611,0.675])

axarr[0].set_ylim([0.792,0.856])

axarr[0].plot([0, 0.04, 0.08],n.array([ 0.83333333, 0.82250521,0.81109048]), label='test1')

axarr[0].errorbar([0, 0.04, 0.08],n.array([ 0.8, 0.83, 0.82]),n.array([0.1,0.1,0.01]), label='test2')

axarr[1].plot([0, 0.04, 0.08],n.array([ 0.66666667, 0.64888304, 0.63042428]))

axarr[1].errorbar([0, 0.04, 0.08],n.array([ 0.67, 0.64, 0.62]),n.array([ 0.01, 0.05, 0.1]))

axarr[0].legend(bbox_to_anchor=(0.04, 0.82, 1., .102),labelspacing=0.1, handlelength=0.1, handletextpad=0.1,frameon=False, ncol=4, columnspacing=0.7)

yuCNw.png

I think what confuses me is that the legend does not actually start at 0.82, and indeed for my larger plot (with 5 subplots of this type), I need to use legend coordinates bbox_to_anchor=(0.04, 1.15, 1., .102) in order to make the legend appear on coordinates (0.02, 0.83). But maybe I am getting something else wrong?

解决方案

The loc parameter specifies in which corner of the bounding box the legend is placed. The default for loc is loc="best" which gives unpredictable results when the bbox_to_anchor argument is used.

Therefore, when specifying bbox_to_anchor, always specify loc as well.

The default for bbox_to_anchor is (0,0,1,1), which is a bounding box over the complete axes. If a different bounding box is specified, is is usually sufficient to use the first two values, which give (x0, y0) of the bounding box.

Below is an example where the bounding box is set to position (0.6,0.5) (green dot) and different loc parameters are tested. Because the legend extents outside the bounding box, the loc parameter may be interpreted as "which corner of the legend shall be placed at position given by the 2-tuple bbox_to_anchor argument".

02303b4f8551e4bb52a299830bd4cde9.png

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = 6, 3

fig, axes = plt.subplots(ncols=3)

locs = ["upper left", "lower left", "center right"]

for l, ax in zip(locs, axes.flatten()):

ax.set_title(l)

ax.plot([1,2,3],[2,3,1], "b-", label="blue")

ax.plot([1,2,3],[1,2,1], "r-", label="red")

ax.legend(loc=l, bbox_to_anchor=(0.6,0.5))

ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes)

plt.tight_layout()

plt.show()

If you want to specify the legend position in other coordinates than axes coordinates, you can do so by using the bbox_transform argument. If may make sense to use figure coordinates

ax.legend(bbox_to_anchor=(1,0), loc="lower right", bbox_transform=fig.transFigure)

It may not make too much sense to use data coordinates, but since you asked for it this would be done via bbox_transform=ax.transData.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值