+2 votes
in Programming Languages by (74.2k 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

+1 vote
by (349k points)
selected by
 
Best answer

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. 


...