Python:位置文本框固定在角落并正确对齐

Gab*_*iel 4 python text matplotlib

我试图模仿这个legend方法,无论轴和盒子的内容如何,我matplotlib.pyplot都可以使用这个方法loc='lower right'固定和正确对齐图例框.

使用text已经出局,因为这需要手动输入坐标,我自动完成.

我尝试过使用注释,它让我在那里走了一半,但它仍然无法正常工作.

这是我到目前为止:

import matplotlib.pyplot as plt

# Define some names and variables to go in the text box.
xn, yn, cod = 'r', 'p', 'abc'
prec = 2
ccl = [546.35642, 6785.35416]
ect = [12.5235, 13.643241]

fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim(-1., 10.)
plt.ylim(-1., 1.)

# Generate text to write.
text1 = "${}_{{t}} = {:.{p}f} \pm {:.{p}f}\; {c}$".format(xn, ccl[0],
    ect[0], c=cod, p=prec)
text2 = "${}_{{t}} = {:.{p}f} \pm {:.{p}f}\; {c}$".format(yn, ccl[1],
    ect[1], c=cod, p=prec)
text = text1 + '\n' + text2

ax.annotate(text, xy=(0.75, 0.9), xycoords='axes fraction', fontsize=10,
    bbox=dict(facecolor='white', alpha=0.8),
    horizontalalignment='left', verticalalignment='bottom')

plt.savefig('annotate_test.png', dpi=150)
Run Code Online (Sandbox Code Playgroud)

这导致:

在此输入图像描述

这将正确缩放以更改轴限制,但问题是:1-如果轴设置为ax.set_aspect('equal'):它将失败:

在此输入图像描述

如果文本太长(这里我prec=5在上面的MWE中设置),它将失败:

在此输入图像描述

我怎么能告诉matplotlib定位文本框的右上角总是和正确对齐,所以它不属于该图像的外部(即:什么loc做的legend)?

Joe*_*ton 9

快速而肮脏的方法是使用右对齐和顶部对齐的文本,并将其放置在距离轴角的点的固定偏移处:

import matplotlib.pyplot as plt

# Define some names and variables to go in the text box.
xn, yn, cod = 'r', 'p', 'abc'
prec = 2
ccl = [546.35642, 6785.35416]
ect = [12.5235, 13.643241]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis([-1, 10, -1, 1])

# Generate text to write.
text1 = "${}_{{t}} = {:.{p}f} \pm {:.{p}f}\; {c}$".format(xn, ccl[0],
    ect[0], c=cod, p=prec)
text2 = "${}_{{t}} = {:.{p}f} \pm {:.{p}f}\; {c}$".format(yn, ccl[1],
    ect[1], c=cod, p=prec)
text = text1 + '\n' + text2

ax.annotate(text, xy=(1, 1), xytext=(-15, -15), fontsize=10,
    xycoords='axes fraction', textcoords='offset points',
    bbox=dict(facecolor='white', alpha=0.8),
    horizontalalignment='right', verticalalignment='top')

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

因为我们已经指定了顶部和右部对齐,所以它适用于您的两个边缘情况:

在此输入图像描述

在此输入图像描述


这样做的缺点是文本是右对齐的.理想情况下,您希望文本对齐与框对齐分开.该matplotlib.offsetbox模块有许多方法来处理这样的事情.

如果您想模仿图例框(下至位置代码),请查看matplotlib.offsetbox.AnchoredText.(请注意,您可以通过padborderpadkwargs 调整填充等:http://matplotlib.org/api/offsetbox_api.html#matplotlib.offsetbox.AnchoredOffsetbox)

import matplotlib.pyplot as plt
import matplotlib.offsetbox as offsetbox

# Define some names and variables to go in the text box.
xn, yn, cod = 'r', 'p', 'abc'
prec = 5
ccl = [546.35642, 6785.35416]
ect = [12.5235, 13.643241]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis([-1, 10, -1, 1])

# Generate text to write.
text1 = "${}_{{t}} = {:.{p}f} \pm {:.{p}f}\; {c}$".format(xn, ccl[0],
    ect[0], c=cod, p=prec)
text2 = "${}_{{t}} = {:.{p}f} \pm {:.{p}f}\; {c}$".format(yn, ccl[1],
    ect[1], c=cod, p=prec)
text = text1 + '\n' + text2

ob = offsetbox.AnchoredText(text, loc=1)
ax.add_artist(ob)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这样做的一个缺点是调整结果的字体和框参数有点违反直觉. AnchoredText接受字体参数字典作为propkwarg.初始化后,可以通过patch属性调整该框.作为一个简单的例子:

ob = offsetbox.AnchoredText(text, loc=1,
                    prop=dict(color='white', size=20))
ob.patch.set(boxstyle='round', color='blue', alpha=0.5)
ax.add_artist(ob)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 它没有很好地记录,但是`AnchoredText`将字体属性的字典作为`prop`kwarg.因此,您可以执行类似`AnchoredText(...,prop = dict(color ='red',alpha = 0.5)`来控制文本颜色和其他属性.框本身的颜色和透明度最容易控制在创建后调整`ob.patch`(例如,`ob.set(alpha = 0.5,color ='blue')`.) (3认同)