Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib text alignment

Is there a way to get the result shown in the third axes with just a single ax.text() command? Using expandtabs almost get me there, but the text never aligns properly.

Using two plotting commands doesn't seem like a good practice to me, and you always need to guess the distance between both, which might take some iterations.

fig, axs = plt.subplots(1,3, figsize=(12,4), 
                       subplot_kw={'aspect': 1, 'xticks': [], 'yticks':[]})
fig.subplots_adjust(wspace=0.05)

values = {'a': 1.35, 'b': 25.1, 'c': 5}

tmpl = """Param1: {a:1.1f}
Long param2: {b:1.1f}
Prm3: {c:1.1f}"""

mystr = tmpl.format(**values)
axs[0].text(0.1, 0.9, mystr, va='top', transform=axs[0].transAxes)
axs[0].set_title('Default')


tmpl = """Param1:\t\t\t{a:1.1f}
Long param2:\t{b:1.1f}
Prm3:\t\t\t{c:1.1f}""".expandtabs()

mystr = tmpl.format(**values)
axs[1].text(0.1, 0.9, mystr, va='top', transform=axs[1].transAxes)
axs[1].set_title('Almost there')


labels = """Param1:
Long param2:
Prm3:"""

tmpl = """{a:1.1f}
{b:1.1f}
{c:1.1f}"""

mystr = tmpl.format(**values)
axs[2].text(0.1, 0.9, labels, va='top', transform=axs[2].transAxes)
axs[2].text(0.65, 0.9, mystr, va='top', ha='right', transform=axs[2].transAxes)
axs[2].set_title('Target')

enter image description here

like image 926
Rutger Kassies Avatar asked Mar 26 '14 14:03

Rutger Kassies


People also ask

How do I align text in MatPlotLib?

MatPlotLib with PythonPlace legend using legend() method and initialize a method. Iterate the legend. get_texts() method to set the horizontal alignment. To display the figure, use show() method.

How do I make text vertical in MatPlotLib?

You can use '\n'.

What is align in MatPlotLib?

(left, right, bottom and top edges) by default. x, height, width, and bottom can be either scalars or sequences. The align and orientation kwargs control the interpretation of x and bottom. The align keyword-only argument controls if x is interpreted as the center or the left edge of the rectangle.

What is BBOX in MatPlotLib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.


1 Answers

Use a monospaced font so that each character takes the same amount of space.

import matplotlib.pyplot as plt

fig, axs = plt.subplots(1,3, figsize=(12,4), 
                       subplot_kw={'aspect': 1, 'xticks': [], 'yticks':[]})
fig.subplots_adjust(wspace=0.05)

values = {'a': 1.35, 'b': 25.1, 'c': 5}

tmpl = """Param1: {a:1.1f}
Long param2: {b:1.1f}
Prm3: {c:1.1f}"""



mystr = tmpl.format(**values)
axs[0].text(0.1, 0.9, mystr, va='top', transform=axs[0].transAxes)
axs[0].set_title('Default')


mono = {'family' : 'monospace'}

textblock = "1234567890\nabcdefghij\nABCDEFGHIJ\n         0"

axs[1].text(0.1, 0.9, textblock, va='top', transform=axs[1].transAxes, fontdict=mono)
axs[1].set_title('Monospaced text block')


axs[2].text(0.1, 0.9, textblock, va='top', transform=axs[2].transAxes)
axs[2].set_title('Not monospaced')

plt.show()

enter image description here

like image 107
Beachcomber Avatar answered Oct 02 '22 05:10

Beachcomber