0

I'm using matplotlib.offsetbox to draw a text box as seen below:

enter image description here

The text box clearly shows too much space at the bottom and not enough at the top.

Why does this happen and how can I fix it?

(Add: I've opened a bug report over at matplotlib's tracker)


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)
Gabriel
  • 40,504
  • 73
  • 230
  • 404

1 Answers1

0

If I understood well , you have to add borderpad

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

UPDATE:

Ok, it seems that the problem is the subscript you are using ath the last line of the box.

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

If you ommit this , and write :

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

it has the same pad top and bottom.

You can use , in order to check , the code here and remove for example the _{{t}} from one line and see the results.

I was able to find only this solution. If someone knows if there is a fix to this ,it will be great.

Community
  • 1
  • 1
George
  • 5,808
  • 15
  • 83
  • 160
  • The `borderpad` option just adds a padding to the whole text box moving it away from the frame's border. It doesn't affect the text box internal padding which is what I need to correct. – Gabriel Mar 16 '15 at 15:55
  • @Gabriel:You want to remove the blank pad at the bottom inside the text box? – George Mar 16 '15 at 15:57
  • Not so much "remove" it as make it of equal size to the pad at the top inside the text box. Right now the bottom padding is noticeably larger than the one at the top. – Gabriel Mar 16 '15 at 16:02
  • I'm aware that the subscript makes the issue appear, but I need that subscript :) The question you link to is also mine, so I'm aware of the code given there. Thank you for the effort! – Gabriel Mar 16 '15 at 16:29
  • 1
    @Gabriel: Ok! One way I thought though , is to insert another text before the text1 with just one empty line and there the padding it's ok.Sth like : `textBefore = '\n'.format(0)` and then `text = textBefore + text1 + ....` – George Mar 17 '15 at 10:08
  • Yeah, that's what I ended up doing. I added a `\n` char to the beginning of `text1` but it's an ugly hack. A proper solution would be nice :( – Gabriel Mar 17 '15 at 12:04