64

I use matplotlib to create a figure with 4 sub-plots in it.

I would like to split one of my title of a subplot, such that each line would be in the centered with respect to subplot.

I tried

import matplotlib.pylab as plt

fig = plt.figure(num=0,figsize=(8.27, 11.69), dpi=300)
ax  = fig.add_subplot(2, 2, 1)
ax.set_title(r'Normalized occupied \\ Neighbors')

and what I get is that Neighbors is indented to the left side.

How could I correct this?

5 Answers 5

113

I get the correct alignment when I format the string this way:

import matplotlib.pylab as plt

fig = plt.figure()#num=0,figsize=(8.27, 11.69), dpi=300)
ax  = fig.add_subplot(2, 2, 1)
ax.set_title('Normalized occupied \n Neighbors')

plt.show()

enter image description here

26

In Matplotlib 3.1.1, both plt.title() and ax.set_title() allow passing text properties as kwargs, so you can pass wrap=True:

plt.title('My long title that I want to wrap and stay center-aligned', wrap=True)

or

fig, [ax1, ax2] = plt.subplots(2, 1)
ax1.set_title('My long title that I want to wrap and stay center-aligned', wrap=True)
1
  • 2
    This should be the correct answer as it will dynamically adjust the title length accordingly to your figure size.
    – Kel Solaar
    Dec 7, 2019 at 23:08
25

A better solution which is compatible with r prefix (especially when one needs to use LaTeX markup) is to divide the title text into two (or more parts), e.g.,

import matplotlib.pylab as plt

fig = plt.figure()
plt.title('My Title\n' + r'$\alpha - \omega$ are LaTeX Markup')
plt.plot([1,2,3,4])
plt.show()
1
  • 3
    thanks for your comment - could I have 2 lines in the title and seperate properties for both? Such as bigger font size and bold for the upper line and normal for the lower?
    – spiff
    Jan 6, 2017 at 1:16
1

In addition to the previous answers, if someone wants to automate the operation I have coded a function to make this easy. here is it:

def split_title_line(title_text, split_on='(', max_words=5):  # , max_words=None):
    """
    A function that splits any string based on specific character
    (returning it with the string), with maximum number of words on it
    """
    split_at = title_text.find (split_on)
    ti = title_text
    if split_at > 1:
        ti = ti.split (split_on)
        for i, tx in enumerate (ti[1:]):
            ti[i + 1] = split_on + tx
    if type (ti) == type ('text'):
        ti = [ti]
    for j, td in enumerate (ti):
        if td.find (split_on) > 0:
            pass
        else:
            tw = td.split ()
            t2 = []
            for i in range (0, len (tw), max_words):
                t2.append (' '.join (tw[i:max_words + i]))
            ti[j] = t2
    ti = [item for sublist in ti for item in sublist]
    ret_tex = []
    for j in range (len (ti)):
        for i in range(0, len(ti)-1, 2):
            if len (ti[i].split()) + len (ti[i+1].split ()) <= max_words:
                mrg = " ".join ([ti[i], ti[i+1]])
                ti = [mrg] + ti[2:]
                break

    if len (ti[-2].split ()) + len (ti[-1].split ()) <= max_words:
        mrg = " ".join ([ti[-2], ti[-1]])
        ti = ti[:-2] + [mrg]
    return '\n'.join (ti)

Examples:

In: split_title_line ('Primary school completion (% of girls)')

out:

Primary school completion 
(% of girls)

In: split_title_line ('Primary school completion in the country as % of girls') Out:

Primary school completion in the
country as % of girls

For your question to split titles in matplotlib or so, you can add this ax.set_title(split_title_line(r'Normalized occupied Neighbors', max_words=2))

Hope that everyone benefits from this.

1
  • Seems useful, thanks! If there was some way to split based on max number of characters that would be swell too :) Jun 18, 2019 at 15:18
0

Would just like to add my 2 cents: the problem here is more general and revolves around correctly inserting newlines to breakup the text from a single long line into a visually pleasing, aesthetic block. Hence, here is my contribution, which is quite similar to Mohammed's and simply splits a string into 5-word chunks. It should be straightforward to customize it to your needs (delimiter to split on, length etc.)

def pretty_name(text):
  words = text.split("_")
  total_string = ""
  for counter, word in enumerate(words):
    if counter>0 and counter % 5 == 0:
      total_string +="\n{}".format(word)
    else:
      total_string +=" {}".format(word)
  return total_string.lstrip()


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.