The seemingly correct way to do that is to instead write
plt.ylabel(r"$\mathit{CYP11B2}$ mRNA (%)")
However, if this works or not will depend on if the font you are using, determined by rcParams['mathtext.it'], has italic numbers or not.
It may be that there is a bug here, but I have not really figured out if it depends on the font or if, for some reasons, digits are not rendered using the italic font, as the default font is not rendered correctly.
I already tried this $\mathit{CYP11B2}$, it doesn't work. I'm currently using Arial and can't change the font type, since it is required by the journal. CYP11B2 needs to be fully italic, this is the biological nomenclature for gene names.
It does seem like we aren't consistent with Latex here. If I do
ax.set_ylabel(r'$\it{abc123}$', usetex=False)
only the letters are in italics, whereas if I do
ax.set_ylabel(r'$\it{abc123}$', usetex=True)
both letters and numbers are in italics.
@Sanas-m the first example above uses Matplotlib's mathtex whereas the second example (usetex=True) uses my locally installed Latex. This may be a workaround for you and you can set an rcParam to turn it on for all your text: https://matplotlib.org/stable/users/explain/text/usetex.html
This looks like a bug to me (matching tex's behavior seems reasonable). The code that forces numbers to be in roman is under UnicodeFonts._get_glyph (commenting out the section starting at if fontname == 'it' and uniindex < 0x10000 revert them to italics), but doing that would turn all digits to italics in math mode (whereas we want r"1\mathit{1}" to only have the second one in italics). I suspect(?) the issue is that at the parsing stage all the characters are forced to italics (more precisely, to rcParams["mathtext.default"] by Parser.non_math), but really that should not affect digits (and greek capital letters); then this info can be threaded all the way down rather than doing the switch in UnicodeFonts._get_glyph.
It does seem like we aren't consistent with Latex here. If I do
ax.set_ylabel(r'$\it{abc123}$', usetex=False)
only the letters are in italics, whereas if I do
ax.set_ylabel(r'$\it{abc123}$', usetex=True)
both letters and numbers are in italics.
@Sanas-m the first example above uses Matplotlib's mathtex whereas the second example (usetex=True) uses my locally installed Latex. This may be a workaround for you and you can set an rcParam to turn it on for all your text: https://matplotlib.org/stable/users/explain/text/usetex.html
I have opened a PR #31121 to fix this, which is waiting for a review.
The fix has two parts:
Introduce normal as the default font in a math environment (can also be explicitly set by \mathnormal{...}) and reserve it for explicit italic (\mathit or \it). This replicates the behaviour of LaTeX.
In the Font classes, set digits in italics or upright when the font is set to 'it' or 'normal', respectively. For cm (Computer Modern), add the text italic (cmti) font and use that for 'it' instead of math italic, same as LaTeX does.
Note that LaTeX further distinguishes between the default and normal math environment, where normal uses oldstyle digits. Modern LaTeX with unicode-math breaks with this behaviour and treats them both the same. For that reason I do not think it is worth the extra complexity.
Activity
oscargus commented on Dec 7, 2024
$ is not for using italic, it is for render text as equations. There characters are italic and numbers not.
oscargus commented on Dec 7, 2024
The seemingly correct way to do that is to instead write
plt.ylabel(r"$\mathit{CYP11B2}$ mRNA (%)")However, if this works or not will depend on if the font you are using, determined by rcParams['mathtext.it'], has italic numbers or not.
It may be that there is a bug here, but I have not really figured out if it depends on the font or if, for some reasons, digits are not rendered using the italic font, as the default font is not rendered correctly.
john-zhang-uoft commented on Dec 7, 2024
Hello, can I work on this if this really is a bug?
Sanas-m commented on Dec 7, 2024
I already tried this $\mathit{CYP11B2}$, it doesn't work. I'm currently using Arial and can't change the font type, since it is required by the journal. CYP11B2 needs to be fully italic, this is the biological nomenclature for gene names.
jklymak commented on Dec 7, 2024
Why are you using mathtext?
oscargus commented on Dec 8, 2024
Is it possible to mix italic and upright fonts in a single label otherwise?
Sanas-m commented on Dec 8, 2024
rcomer commented on Dec 8, 2024
It does seem like we aren't consistent with Latex here. If I do
only the letters are in italics, whereas if I do
both letters and numbers are in italics.
@Sanas-m the first example above uses Matplotlib's mathtex whereas the second example (usetex=True) uses my locally installed Latex. This may be a workaround for you and you can set an rcParam to turn it on for all your text:
https://matplotlib.org/stable/users/explain/text/usetex.html
jklymak commented on Dec 8, 2024
Sorry, agree you need something tex-like if you want to mix italic and non-italics.
anntzer commented on Dec 9, 2024
This looks like a bug to me (matching tex's behavior seems reasonable). The code that forces numbers to be in roman is under UnicodeFonts._get_glyph (commenting out the section starting at if fontname == 'it' and uniindex < 0x10000 revert them to italics), but doing that would turn all digits to italics in math mode (whereas we want r"1\mathit{1}" to only have the second one in italics). I suspect(?) the issue is that at the parsing stage all the characters are forced to italics (more precisely, to rcParams["mathtext.default"] by Parser.non_math), but really that should not affect digits (and greek capital letters); then this info can be threaded all the way down rather than doing the switch in UnicodeFonts._get_glyph.
Sanas-m commented on Dec 10, 2024
I tried this but then the fonts are mixed again
NaotoKubota commented on Apr 15, 2025
Same problem here. I would be grateful if someone could fix this.
cmatKhan commented on Apr 28, 2025
bump -- found this after struggling to correctly format gene symbols.
Edit -- this ended up working for me (with a good deal of help from chatGPT):
On ubuntu 22.04, I installed the following
and then the following matplotlib settings:
that made this work:
llohse commented on Feb 7, 2026
This appears to be a logic bug in _mathtext.py.
The Parser class sets fontname to 'it' as soon as a math environment is entered.
matplotlib/lib/matplotlib/_mathtext.py
Lines 2197 to 2204 in 45caf0e
Later, when the actual glyph is requested for rendering, it will only italize actual non-greek letters:
matplotlib/lib/matplotlib/_mathtext.py
Lines 619 to 623 in 45caf0e
One way to fix this would be to
llohse commented on Feb 10, 2026
I have opened a PR #31121 to fix this, which is waiting for a review.
The fix has two parts:
Note that LaTeX further distinguishes between the default and normal math environment, where normal uses oldstyle digits. Modern LaTeX with unicode-math breaks with this behaviour and treats them both the same. For that reason I do not think it is worth the extra complexity.
QuLogic commented on Apr 10, 2026
The text-overhaul branch is now merged to main.