调整 matplotlib 注释框内的填充 [英] Adjust padding inside matplotlib annotation box

查看:40
本文介绍了调整 matplotlib 注释框内的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Axes 对象上使用 annotate 方法将带有文本的箭头添加到绘图中.例如:

I'm using the annotate method on an Axes object to add an arrow with text to a plot. For example:

ax.annotate('hello world,
            xy=(1, 1),
            xycoords='data',
            textcoords='data',
            fontsize=12,
            backgroundcolor='w',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3")

这很有效,但我想减少注释框内部的填充.从本质上讲,我想使文本框紧缩.有没有办法通过 arrowprops bbox_props kwargs做到这一点?

This works well but I want to decrease the padding on the inside of the annotation box. Essentially, I want to make the box 'squeeze' tighter around the text. Is there any way to do this via the arrowprops or bbox_props kwargs?

我正在寻找诸如 borderpad 之类的东西,它可以在图例中使用,类似于在这个答案中讨论的 .

I'm looking for something like borderpad that's available on legends, similar to what's discussed on this answer.

推荐答案

是的,但是您需要切换到一种稍微不同的方式来指定该框.基本"框不支持它,因此您需要让 annotate 制作一个与文本对象关联的 FancyBboxPatch.(花式"框的相同语法也适用于放置在 ax.text 中的文本,无论其价值如何.)

Yes, but you'll need to switch to a slightly different way of specifying the box. The "basic" box doesn't support it, so you need to have annotate make a FancyBboxPatch associated with the text object. (The same syntax for a "fancy" box would work text placed with ax.text as well, for what it's worth.)

此外,在更进一步之前,在当前版本的matplotlib(1.4.3)中,有几个相当棘手的错误会影响到这一点.(例如 https://github.com/matplotlib/matplotlib/issues/4139 https://github.com/matplotlib/matplotlib/issues/4140)

Also, before we go much farther, there are a couple of rather thorny bugs that affect this in the current version of matplotlib (1.4.3). (e.g. https://github.com/matplotlib/matplotlib/issues/4139 and https://github.com/matplotlib/matplotlib/issues/4140)

如果您看到这样的事情:

If you're seeing things like this:

而不是这个:

您可以考虑降级到matplotlib 1.4.2,直到问题解决.

You might consider downgrading to matplotlib 1.4.2 until the issue is fixed.

让我们以您的榜样为起点.我已将背景色更改为红色,并将其放置在图形的中央,以使其更容易看清.我也将离开箭头(避免上面的错误),而只使用 ax.text 而不是 annotate .

Let's take your example as a starting point. I've changed the background color to red and put it in the center of the figure to make it a touch easier to see. I'm also going to leave off the arrow (avoiding the bug above) and just use ax.text instead of annotate.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
a = ax.text(0.5, 0.5, 'hello world',
            fontsize=12,
            backgroundcolor='red')

plt.show()

要更改填充,您需要将 bbox kwarg用于 text (或 annotate ).这使文本使用 FancyBboxPatch ,它支持填充(以及其他一些功能).

To be able to change the padding, you'll need to use the bbox kwarg to text (or annotate). This makes the text use a FancyBboxPatch, which supports padding (along with several other things).

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
a = ax.text(0.5, 0.5, 'hello world', fontsize=12,
            bbox=dict(boxstyle='square', fc='red', ec='none'))

plt.show()

默认填充为 pad = 0.3 .(如果我没记错的话,单位是文本范围的高度/宽度的分数.)如果您想增加它,请使用 boxstyle='square,pad=':

The default padding is pad=0.3. (If I recall correctly, the units are fractions of the height/width of the text's extent.) If you'd like to increase it, use boxstyle='square,pad=<something_larger>':

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
a = ax.text(0.5, 0.5, 'hello world', fontsize=12,
            bbox=dict(boxstyle='square,pad=1', fc='red', ec='none'))

plt.show()

或者您可以通过输入 0 或负数来减少它以进一步缩小它:

Or you can decrease it by putting in 0 or a negative number to shrink it farther:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
a = ax.text(0.5, 0.5, 'hello world', fontsize=12,
            bbox=dict(boxstyle='square,pad=-0.3', fc='red', ec='none'))

plt.show()

这篇关于调整 matplotlib 注释框内的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆