How do I fit long title?

  • matplotlib: Auto-wrapping text is the official answer from the matplotlib documentation
    • Review the Note for associated caveats.
  • matplotlib: Text properties and layout are also useful
  • It doesn't work to display an inline plot in Jupyter because of GitHub: Text wrapping doesn't seem to work in jupyter notebook #10869 unless you do this answer Matplotlib and Ipython-notebook: Displaying exactly the figure that will be saved
    • plt.savefig(...) seems to work properly with wrap
import matplotlib.pyplot as plt

x = [1,2,3]
y = [4,5,6]

# initialization:
fig, axes = plt.subplots(figsize=(8.0, 5.0))

# title:
myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."

# lines:
axes.plot(x, y)

# set title
axes.set_title(myTitle, loc='center', wrap=True)

plt.show()

enter image description here

  • The following also works
plt.figure(figsize=(8, 5))

# title:
myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."

# lines:
plt.plot(x, y)

# set title
plt.title(myTitle, loc='center', wrap=True)

plt.show()

Note

  • The following way of adding an axes is deprecated
# lines:
fig.add_subplot(111).plot(x, y)

# title:
myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."

fig.add_subplot(111).set_title(myTitle, loc='center', wrap=True)

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.


Here's what I've finally used:

#!/usr/bin/env python3

import matplotlib
from matplotlib import pyplot as plt
from textwrap import wrap

data = range(5)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(data, data)

title = ax.set_title("\n".join(wrap("Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all.", 60)))

fig.tight_layout()
title.set_y(1.05)
fig.subplots_adjust(top=0.8)

fig.savefig("1.png")

enter image description here


One way to do it is to simply change the font size of the title:

import pylab as plt

plt.rcParams["axes.titlesize"] = 8

myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."
plt.title(myTitle)
plt.show()

enter image description here

In the answer you linked are several other good solutions that involve adding newlines. There is even an automatic solution that resizes based off of the figure!


I preferred to adapt @Adobe's solution in this way:

plt.title("First Title\n%s" % "\n".join(wrap("Second Title", width=60)))

Tags:

Matplotlib