2

I have seen this issue pop up here and there but have yet to find a suitable answer.

When making a plot in matplotlib, the only way to insert symbols and math functions (like fractions, exponents, etc...) is to use TeX formatting. However, by default TeX formatting uses a different font AND italicizes the text. So for example, if I wanted an axis label to say the following:

photons/cm^2/s/Angstrom

I have to do the following:

ax1.set_ylabel(r'Photons/$cm^2$/s/$\AA$')

This produces a very ugly label that uses 2 different fonts and has bits and pieces italicized.

How do I permanently change the font of TeX (Not the other way around) so that it matches the default font used by matplotlib?

I have seen other solutions that tell the user to manually make all text the same in a plot by using \mathrm{} for example but this is ridiculously tedious. I have also seen solutions which change the default font of matplotlib to match TeX which seem utterly backwards to me.

2 Answers 2

1

It turns out the solution was rather simple and a colleague of mine had the solution.

If I were to use this line of code to create a title:

fig.suptitle(r'$H_2$ Emission from GJ832')

The result would be "H2 Emission from GJ832" which is an illustration of the problem I was having. However, it turns out anything inside of the $$ is converted to math type and thus the italics assigned.

If we change that line of code to the following:

fig.suptitle(r'H$_2$ Emission from GJ832')

Then the result is "H2 Emission from GJ832" without the italics. So this is an example of where we can constrain the math type to include only the math parts of the text, namely creating the subscript of 2.

However, if I were to change the code to the following:

fig.suptitle(r'H$_{two}$ Emission from GJ832')

the result would be "Htwo Emission from GJ832" which introduces the italics again. In this case, and for any case where you must have text (or are creating unit symbols) inside the dollar signs, you can easily remove the italics the following way:

fig.suptitle(r'H$_{\rm two}$ Emission from GJ832')

or in the case of creating a symbol:

ax2.set_xlabel(r'Wavelength ($\rm \AA$)')

The former results in "Htwo Emission from GJ832"

and the latter in "Wavelength (A)" where A is the Angstrom symbol.

Both of these produce the desired result with nothing italicized by calling \rm before the text or symbol in the dollar signs. The result is nothing italicized INCLUDING the Angstrom symbol created by \AA.

While this doesn't change the default of the TeX formatting, it IS a simple solution to the problem and doesn't require any new packages. Thank you Roland Smith for the suggestions anyway. I hope this helps others who have been struggling with the same issue.

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

Comments

0

For typesetting units, use the siunitx package (with mode=text) rather than math mode.

Update: The above is only valid when you have defined text.usetex : True in your rc settings.

From the matplotlib docs:

Note that you do not need to have TeX installed, since matplotlib ships its own TeX expression parser, layout engine and fonts.

And:

Regular text and mathtext can be interleaved within the same string. Mathtext can use the Computer Modern fonts (from (La)TeX), STIX fonts (with are designed to blend well with Times) or a Unicode font that you provide. The mathtext font can be selected with the customization variable mathtext.fontset

Reading this, it sounds that setting mathtext.fontset and the regular font that matplotlib uses the same would solve the problem if you don't use TeX.

3 Comments

That isn't a bad solution actually, apart from needing to learn new syntax. However, I am not working in SI units but rather cgs units (there are only a couple of units I don't see in the package, but they happen to be some I use frequently). Thanks for the reply!
@Will.Evo You might not be using TeX. See updated answer.
Roland, Thanks for the reply. I was using TeX and found a very simple solution to the problem. Thanks for the help!

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.