matplotlib中的offsetbox底部有太多空间

时间:2015-03-16 15:22:10

标签: python matplotlib

我使用matplotlib.offsetbox绘制文本框,如下所示:

enter image description here

文本框清楚地显示底部空间太大而顶部空间不足。

为什么会发生这种情况,我该如何解决?

(添加:我已在matplotlib的跟踪器上打开了bug report


MWE

import matplotlib.pyplot as plt
import matplotlib.offsetbox as offsetbox
import matplotlib.gridspec as gridspec
import random

def scatter_plot(x, y):

    ax = plt.subplot(gs[0:2, 0:2])
    # Add text box
    text1 = '$N = {}$\n'.format(0)
    text2 = '$a = {} \pm {}$\n'.format(0.01, 0.01)
    text3 = '$log(b) = {} \pm {}$\n'.format(1.2, 0.3)
    text4 = '$C_{{(aaa)}} = {} \pm {}$\n'.format(0.2, 0.05)
    text5 = '$D_o = {} \pm {}$\n'.format(10.6, 0.7)
    text6 = '$E_{{\odot}} = {} \pm {}$\n'.format(100., 200.)
    text7 = '$F_{{fff}} = {} \pm {}$'.format(0.5, 0.3)
    text = text1 + text2 + text3 + text4 + text5 + text6 + text7
    ob = offsetbox.AnchoredText(text, loc=1, prop=dict(size=12))
    ob.patch.set(boxstyle='square,pad=0.2', alpha=0.85)
    ax.add_artist(ob)

    plt.scatter(x, y)

# Generate random data.
x = [random.random() for i in xrange(100)]
y = [random.random() for i in xrange(100)]
# Define size of output figure.
fig = plt.figure(figsize=(30, 25))  # create the top-level container
gs = gridspec.GridSpec(10, 12)      # create a GridSpec object
# Create plot.
scatter_plot(x, y)
# Save plot to file.
fig.tight_layout()
plt.savefig('out.png', dpi=150)

1 个答案:

答案 0 :(得分:0)

如果我理解得很清楚,你必须添加borderpad

ob = offsetbox.AnchoredText(text, loc=1, borderpad = 2.5,prop=dict(size=12))

<强>更新

好吧,问题似乎是你在框的最后一行使用的下标。

text7 = '$F_{{fff}} = {} \pm {}$'.format(0.5, 0.3)

如果你省略了这一点,请写下:

text7 = '$F = {} \pm {}$'.format(0.5, 0.3)

它有相同的垫顶部和底部。

您可以使用,以便检查代码here,并从一行中删除_{{t}}并查看结果。

我只能找到这个解决方案。 如果有人知道是否有解决方法,那就太棒了。