我正在使用annotate
方法对Axes
对象添加一个带文本的箭头到绘图。例如:
ax.annotate('hello world,
xy=(1, 1),
xycoords='data',
textcoords='data',
fontsize=12,
backgroundcolor='w',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3")
这很好,但我想减少注释框内部的填充。基本上,我想把文本框“挤压”得更紧。有什么方法可以通过arrowprops
或bbox_props
kwargs来实现这一点吗?
我正在寻找像borderpad
这样的东西,可以在传说中找到,类似于讨论的on this answer。
是的,但是您需要切换到稍微不同的方式来指定框。“basic”框不支持它,因此需要让
annotate
与文本对象关联FancyBboxPatch
。(同样的语法对于一个“花哨的”框也适用于放在ax.text
上的文本,不管它值多少钱。)另外,在我们进一步讨论之前,在当前版本的matplotlib(1.4.3)中有几个相当棘手的错误会影响到这一点。(例如https://github.com/matplotlib/matplotlib/issues/4139和https://github.com/matplotlib/matplotlib/issues/4140)
如果你看到这样的事情:
而不是这个:
在问题解决之前,可以考虑降级到matplotlib 1.4.2。
让我们以你的例子为起点。我把背景颜色改成了红色,并把它放在图的中心,使它更容易被看到。我还将去掉箭头(避免上面的错误),只使用
ax.text
,而不是annotate
。要更改填充,您需要使用
bbox
kwarg来text
(或annotate
)。这使得文本使用FancyBboxPatch
,它支持填充(以及其他一些东西)。默认填充为
pad=0.3
。(如果我没记错的话,单位是文本范围的高度/宽度的分数。)如果要增加,请使用boxstyle='square,pad=<something_larger>'
:或者可以通过放入
0
或负数来进一步缩小:相关问题 更多 >
编程相关推荐