23

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.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Aaron Rosenberg
  • 333
  • 1
  • 3
  • 8

3 Answers3

22

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.

marisano
  • 571
  • 6
  • 10
  • Got error `RuntimeError: LaTeX was not able to process the following string: 'lp'` in Mac. Seems the [solution is to install texlive-latex-extra](http://stackoverflow.com/a/11357765/3998252). I tried it but didn't work me. – Manavalan Gajapathy May 05 '17 at 23:29
  • @JeeYem Seems to be a Mac matplotlib + python problem.Try having a look at this post: https://github.com/dfm/daft/issues/65 . – marisano May 09 '17 at 05:46
  • `...selectfont{}(September 16...` seems to miss a `{` before `(September...`. – Maosi Chen Jul 15 '20 at 20:58
  • Or maybe the error is because of: [https://github.com/matplotlib/matplotlib/issues/10317](https://github.com/matplotlib/matplotlib/issues/10317) – Xopi García Dec 09 '20 at 14:10
  • @MaosiChen You were correct about the braces being amiss, but rather than there being a missing brace, there was an extra one: the brace just after `r'`, or rather the one that was there. I've since edited and corrected the code of the original answer. – marisano Dec 11 '20 at 11:05
  • 1
    It can help to remove problematic characters (like `\r`,`\n`, etc) to get it working, then adapt: i.e. `t = r'\fontsize{30pt}{3em}\selectfont{}{SomeTitle}'` -> `plt.title( t )` – pds May 22 '21 at 12:39
  • @pds That is true. However, the real culprit I had had was that I was using the original WSL (Windows Subsystem for Linux) under Windows 10, which would spit out a huge number of messages when interfaced graphically - which is still not officially supported but works fine. Most of those were inconsequential, but unfortunately, that corresponding error message had gotten lost. However, it was easy to see once WSL 2 was available and I'd updated it. – marisano May 23 '21 at 15:30
9

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.

Anzel
  • 19,825
  • 5
  • 51
  • 52
  • 2
    In my case, title and suptitle locates on the same line, that is , they are overlapped. How to control the relative position between them? – Joonho Park Mar 15 '19 at 02:27
  • Do you mean you want to separate the lines? You can try using line breaks, that is “\n” – Anzel Mar 15 '19 at 10:03
  • To be precise, two lines are different objects but it locates on the same position for title and suptitle. But it works. Using "\n" in suptitle, it can locate under title object. That's good technique. – Joonho Park Mar 19 '19 at 01:26
  • 1
    You can also add a "y" parameter for title or suptitle, indicating its vertical position with 0 --> 1 going bottom --> top: `plt.title(title1, fontsize=12, y=0.95)` – hekimgil May 26 '20 at 04:01
0

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.

Tzane
  • 2,752
  • 1
  • 10
  • 21
blitzoc
  • 11
  • 2