'How do I fit long title?
There's a similar question - but I can't make the solution proposed there work.
Here's an example plot with a long title:
#!/usr/bin/env python
import matplotlib
import matplotlib.pyplot
import textwrap
x = [1,2,3]
y = [4,5,6]
# initialization:
fig = matplotlib.pyplot.figure(figsize=(8.0, 5.0))
# 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("\n".join(textwrap.wrap(myTitle, 80)))
# tight:
(matplotlib.pyplot).tight_layout()
# saving:
fig.savefig("fig.png")
it gives a
AttributeError: 'module' object has no attribute 'tight_layout'
and if I replace (matplotlib.pyplot).tight_layout() with fig.tight_layout() it gives:
AttributeError: 'Figure' object has no attribute 'tight_layout'
So my question is - how do I fit the title to the plot?
Solution 1:[1]
- matplotlib: Auto-wrapping text is the official answer from the
matplotlibdocumentation- 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 withwrap
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()
- 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.
Solution 2:[2]
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()

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!
Solution 3:[3]
I preferred to adapt @Adobe's solution in this way:
plt.title("First Title\n%s" % "\n".join(wrap("Second Title", width=60)))
Solution 4:[4]
Add \n in your text title
Before
axs[0, 0].set_title('pure imshow of 2D-array (RGB)')
axs[0, 1].set_title('Mean filter') # imshow by default applies a standard colormap, viridis cmap, which is greenish.
axs[0, 2].set_title('pure imshow of 2D-array (R-channel)') #
axs[1, 0].set_title('imshow of 2D-array with R values cmap="Grey_r"')
axs[1, 1].set_title('imshow of 2D-array with R values cmap="Reds_r"')
axs[1, 2].set_title('imshow of 3D-array with coordinates 1 and 2 \n(i.e.: channels G and B) set to 0')
After
axs[1, 0].set_title('imshow of 2D-array \n with R values cmap="Grey_r"')
axs[1, 1].set_title('imshow of 2D-array \n with R values cmap="Reds_r"')
axs[1, 2].set_title('imshow of 3D-array with coordinates 1 and 2 \n channels G and B) set to 0')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Community |
| Solution 3 | Luca Urbinati |
| Solution 4 | ÙAbdalrahman M. Amer |

