18

I'm trying to show an image with a title that combines normal text with a couple of Latex commands:

plt.imshow(w1subtracted2, origin='lower',
          extent=[l_max, -l_max, -b_max, b_max],
          cmap = color_map)
plt.title('W1 disk and central $\pm2\textdegree$ subtracted', fontsize = 'small')
plt.xlabel(xlabel, fontsize = 'small')
plt.ylabel(ylabel, fontsize = 'small')
plt.savefig('w1subtracted2.png')

but I can't get the title showing the \pm and \textdegree to show in Latex style. How can I achieve that?

1 Answer 1

32

It seems you are not using latex (usetex = True) but simple MathText. There is no \textdegree in MathText.

Using

plt.title(r'W1 disk and central $\pm2^\circ$ subtracted', fontsize='small')

should give you

enter image description here

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

4 Comments

and where should usetex = True go?
In matplotlib's rcParams. See the documentation here: matplotlib.org/stable/tutorials/text/usetex.html
This are the required lines for this answer to work: import matplotlib as mpl ; mpl.rcParams['text.usetex'] = True
I like how this answer encourages solutions that don't use usetex = True. The built-in MathText seems cleaner where it will work.

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.