Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Numbers in words not italic #29253

Open
Sanas-m opened this issue Dec 7, 2024 · 11 comments
Open

[Bug]: Numbers in words not italic #29253

Sanas-m opened this issue Dec 7, 2024 · 11 comments

Comments

@Sanas-m
Copy link

Sanas-m commented Dec 7, 2024

Bug summary

Y labels in plots cannot be displayed in italic if the label contains numbers. While the letters will be italic, the numbers will stay in normal font.

Code for reproduction

mpl.rcParams['font.family'] = 'Arial'

bars = sns.barplot(
    x='Sample Name',
    y='delta',
    data=renamed,
    palette=custom_palette,
    alpha=1,
    edgecolor='grey',
    errorbar='se',
    errcolor='black',
    capsize=0.05,
    errwidth=1
)


sns.stripplot(
    x='Sample Name',
    y='delta',
    data=renamed,
    palette=custom_palette,
    edgecolor='white',
    linewidth=1,
    alpha=1,
    size=8,
    legend=False,
    dodge=False,
    jitter=0.17
)

bars.spines['top'].set_visible(False)
bars.spines['right'].set_visible(False)
plt.xticks(rotation=45, ha='right')
plt.ylabel("${CYP11B2}$ mRNA (%)")
plt.xlabel("")
plt.suptitle("")

plt.show()

Actual outcome

figure

Expected outcome

image

Additional information

No response

Operating system

Mac OS Apple M1 Ventura 13.4.1

Matplotlib Version

3.9.2

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.11.3

Jupyter version

6.5.4

Installation

pip

@oscargus
Copy link
Member

oscargus commented Dec 7, 2024

$ is not for using italic, it is for render text as equations. There characters are italic and numbers not.

@oscargus
Copy link
Member

oscargus commented 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
Copy link

john-zhang-uoft commented Dec 7, 2024

Hello, can I work on this if this really is a bug?

@Sanas-m
Copy link
Author

Sanas-m commented Dec 7, 2024

plt.ylabel(r"$\mathit{CYP11B2}$ mRNA (%)")

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
Copy link
Member

jklymak commented Dec 7, 2024

Why are you using mathtext?

@oscargus
Copy link
Member

oscargus commented Dec 8, 2024

Why are you using mathtext?

Is it possible to mix italic and upright fonts in a single label otherwise?

@Sanas-m
Copy link
Author

Sanas-m commented Dec 8, 2024 via email

@rcomer
Copy link
Member

rcomer commented Dec 8, 2024

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

@jklymak
Copy link
Member

jklymak commented Dec 8, 2024

Why are you using mathtext?

Is it possible to mix italic and upright fonts in a single label otherwise?

Sorry, agree you need something tex-like if you want to mix italic and non-italics.

@anntzer
Copy link
Contributor

anntzer commented 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
Copy link
Author

Sanas-m commented Dec 10, 2024

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 tried this but then the fonts are mixed again

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants