Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suptitle alignment issues in Matplotlib

I would like to align my suptitle in the top left-hand corner on each page of the multiple PDF document I am trying to create, however, it doesn't seem to be doing as I try to ask. The below script produces the suptitle in the top and centre. I cannot figure out what I am doing wrong:

import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

pdf = PdfPages('TestPDF.pdf')

Countries = ['Argentina', 'Australia']

for country in Countries:
    fig = plt.figure(figsize=(4, 5))
    fig.set_size_inches([10, 7])
    plt.suptitle(country, horizontalalignment='left', verticalalignment='top',
                 fontsize = 15)
    x = 1000
    plt.plot(x, x*x, 'ko')
    pdf.savefig()
    plt.close(fig)

pdf.close()

Strangely:

verticalalignment='bottom'

only shifts the suptitle higher (and off the top of the page slightly), which makes me think that it is aligning itself off different boundaries than the edges of the page?

like image 709
Curious Student Avatar asked Oct 26 '17 11:10

Curious Student


People also ask

How to align the title of a subplot to the figure?

So if you want to have a suptitle aligned with the (only) subplot in a figure you may set its position according to the subplot paramters, e.g. the left parameter ( fig.subplotpars.left) is 0.125. You will still need to set the aligment as well.

How to add a title to the figure in Matplotlib using Python?

The suptitle () function in pyplot module of the matplotlib library is used to add a title to the figure. Parameters: This function will have following parameters: t : The title text you want to add to your graph. x : The x location of the text in figure coordinates. It’s default value is 0.5. y : The y location of the text in figure coordinates.

How to get the position of a suptitle in a graph?

The suptitle does not have a loc parameter. In order to position a suptitle you may however use the x and y arguments. The position is in figure coordinates.

What is Matplotlib in Python?

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements.


1 Answers

From https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.suptitle.html:

Add a centered title to the figure.

kwargs are matplotlib.text.Text properties. Using figure coordinates, the defaults are:

x : 0.5

   The x location of the text in figure coords

y : 0.98

   The y location of the text in figure coords

You can move the position of the suptitle in figure coordinates by choosing different values for x and y. For example,

plt.suptitle(country, x=0.1, y=.95, horizontalalignment='left', verticalalignment='top', fontsize = 15)

will put the suptitle in the upper left corner.

The keywords horizontalalignment and verticalalignment work a bit different ( see e.g. https://matplotlib.org/users/text_props.html):

horizontalalignment controls whether the x positional argument for the text indicates the left, center or right side of the text bounding box.

verticalalignment controls whether the y positional argument for the text indicates the bottom, center or top side of the text bounding box.

like image 128
jdamp Avatar answered Oct 19 '22 08:10

jdamp