Aaron Rosenberg
Aaron Rosenberg

Reputation: 333

Multiple font sizes in plot title

I'm plotting in an IPython IDE using Matplotlib.pyplot and added a title with:

plt.title('Mean WRFv3.5 LHF\n(September 16 - October 30, 2012)', fontsize=40)

However, I want the first line to be size 40 and the second line to be size 18. Is that possible in matplotlib? I saw the LaTeX use of \tiny and \Huge, but would like more control.

Upvotes: 23

Views: 18873

Answers (3)

blitzoc
blitzoc

Reputation: 21

From here: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/figure_title.html

You could use it like:

fig, (ax1, ax2) = plt.subplots(1, 2, layout='constrained', sharey=True)
ax1.set_title('damped')
fig.suptitle('Different types of oscillations', fontsize=16)

Like this the two different titles should not superimpose on each other.

Upvotes: 0

marisano
marisano

Reputation: 591

Try:

import matplotlib.pyplot as plt

plt.rc('text', usetex=True)
plt.title(r'\fontsize{30pt}{3em}\selectfont{}{Mean WRFv3.5 LHF\r}{\fontsize{18pt}{3em}\selectfont{}(September 16 - October 30, 2012)}')

plt.show()

Single label with text of two different sizes.

That \r might want to be a \n on your system.

I used Joel's answer to address your question.

Upvotes: 23

Anzel
Anzel

Reputation: 20553

Not sure if this is what you want, but you may add a suptitle or text and set at different fontsize like this:

plt.title('Mean WRFv3.5 LHF\n', fontsize=40)
plt.suptitle('(September 16 - October 30, 2012)\n', fontsize=18)
plt.text(0.5, 1, 'the third line', fontsize=13, ha='center')

enter image description here

Hope this helps.

Upvotes: 9

Related Questions