设置AnchoredOffsetbox的线宽和面色?

时间:2015-11-10 16:13:57

标签: python matplotlib

是否可以更改AnchoredOffsetbox的linewidth,facecolor等?

我已经用它来列出我的情节旁边的一些变量(例如' A = 1',' B = 2'),以这种方式#39; ='是垂直对齐的,所以它有点像额外的图例(但手柄也是文字)。但我无法弄清楚如何像传奇一样设置框架的属性。

非常感谢任何提示!

代码:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, HPacker, VPacker

fig = plt.figure(1, figsize=(3,3))
ax = plt.subplot(111)

p_txt0 = ['A = ', 'BBB = ']
p_num0 = [1.0, 2.0]
p_txt = [TextArea(text, textprops=dict(size='medium',weight='bold')) 
         for text in p_txt0]
p_num = [TextArea('%6.2f' % num, textprops=dict(size='medium',weight='bold')) 
         for num in p_num0]
p_vbox_txt = VPacker(children=p_txt, align="right", pad=0, sep=5)
p_vbox_num = VPacker(children=p_num, align="right", pad=0, sep=5)
box = HPacker(children=[p_vbox_txt,p_vbox_num], align="center", pad=5, sep=3)

parambox = AnchoredOffsetbox(loc=2, child=box, pad=0.0, frameon=True, 
                             borderpad=0.0, bbox_to_anchor=(1.1, 1.0), 
                             bbox_transform=ax.transAxes)

parambox.set_clip_on(False) #so box won't be cut off when saving
ax.add_artist(parambox)

plt.savefig('offsetbox.png', bbox_extra_artists=(parambox,), bbox_inches='tight')

example

1 个答案:

答案 0 :(得分:1)

您似乎可以访问parambox补丁,并在其上设置属性:

ax.add_artist之前,请尝试以下几行:

parambox.patch.set_linewidth(4)
parambox.patch.set_edgecolor('r')
parambox.patch.set_facecolor('g')

enter image description here

我认为这是设置基础matplotlib.patches.Patch的属性,因此您可以查看here以了解可以更改的更多选项(例如set_alphaset_linestyle