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.

3 Answers 3

23

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Got error RuntimeError: LaTeX was not able to process the following string: 'lp' in Mac. Seems the solution is to install texlive-latex-extra. I tried it but didn't work me.
@JeeYem Seems to be a Mac matplotlib + python problem.Try having a look at this post: github.com/dfm/daft/issues/65 .
...selectfont{}(September 16... seems to miss a { before (September....
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 )
|
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.

4 Comments

In my case, title and suptitle locates on the same line, that is , they are overlapped. How to control the relative position between them?
Do you mean you want to separate the lines? You can try using line breaks, that is “\n”
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.
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)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.