+2 votes
in Programming Languages by (8.1k points)

I have to put a long title on my plot. I want to wrap the title so that it fits perfectly. How can I wrap the long title in a plot? My title looks like this: 

title = 'Coded and inferred suicidality/self-harm seen in inpatient/ER meta-visits by state for patients with major mental illness'

1 Answer

0 votes
by (15.8k points)

You can wrap the title using wrap() module.  Check the following example to understand how to use the wrap() function.

import matplotlib.pyplot as plt

from textwrap import wrap

title = 'Coded and inferred suicidality/self-harm seen in inpatient/ER meta-visits by state for patients with major mental illness'

plt.title('\n'.join(wrap(title,60)), fontsize=14)

Here I have used size '60' to wrap. You can change it according to your plot. 

...