Skip to content

[Bug]: latex font doesn't apply to formatted ticks #26179

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

Open
hahmadi-cmu-F24 opened this issue Jun 23, 2023 · 5 comments
Open

[Bug]: latex font doesn't apply to formatted ticks #26179

hahmadi-cmu-F24 opened this issue Jun 23, 2023 · 5 comments

Comments

@hahmadi-cmu-F24
Copy link

Bug summary

When latex is turned on and tick format uses specific formatters (i.e logformattermathtext, scalarformatter with scientific tuned on), the font doesn't apply and switches back to serif.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "sans-serif"
})

fig, ax = plt.subplots()
plt.plot(range(10))

plt.title('Center Title')
plt.title('Left Title', loc='left')
plt.title('Right Title', loc='right')

formatter=ticker.ScalarFormatter()
formatter.set_scientific(False)
ax.yaxis.set_major_formatter(formatter)

ax.xaxis.set_minor_formatter(ticker.FormatStrFormatter('%s'))
ax.set_xticklabels(ax.get_xticks())

plt.show()

Actual outcome

deletethiss

Expected outcome

The yticks should match the xtick/labels font (sans-serif) even though the formatting is different.

Additional information

No response

Operating system

Linux

Matplotlib Version

3.5

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

pip

@hahmadi-cmu-F24 hahmadi-cmu-F24 changed the title [Bug]: latex font doesn't apply to math/sci format ticks [Bug]: latex font doesn't apply to formatted ticks Jun 23, 2023
@oscargus oscargus added Community support Users in need of help. topic: text/usetex and removed Community support Users in need of help. labels Jun 24, 2023
@oscargus
Copy link
Member

The problem is that setting the font to sans-serif, will not set the math-font to sans-serif. See https://stackoverflow.com/questions/2537868/sans-serif-math-with-latex-in-matplotlib for solutions.

As ScalarFormatter always uses an equation when formatting when usetex is used, the math font will be used. You can try this out by e.g. formatter(2) in your example.

To get around this, you can use the FormatStrFormatter just as you have done (but set it for the major ticks and do not call set_xticklabels).

@hahmadi-cmu-F24
Copy link
Author

I appreciate you looking into this. I understand math text needs to be set separately, the problem I've been having is mathtext.fontset doesn't work at all so I was trying to figure out a way to turn the math text into a string in order to keep any previously set formatter (which both my and your solution's don't do since formatstrformatter can remove exponents/symbols from the previously set formatter).
I can open an issue for math text not setting instead, but I also wanted to see if this way is possible because it's simpler for my code than adding if statements for each possible user input for the font. Please let me know your thoughts, thanks!

@ksunden
Copy link
Member

ksunden commented Jun 28, 2023

the mathtext.fontset rc param controls the behavior of the built-in (to matplotlib) mathtext (usetex=False) implementation of a subset of LaTeX math mode, not the font when using the full usetex=True LaTeX.

Ref: https://matplotlib.org/stable/users/prev_whats_new/dflt_style_changes.html#math-text

@ksunden
Copy link
Member

ksunden commented Jun 28, 2023

If your usage is supported by mathtext (without calling out to full LaTeX), then that may solve your immediate issue. otherwise it will likely mean some combination of tex.preamble and wrapping your fragments with e.g. \mathsf{...}.

@hahmadi-cmu-F24
Copy link
Author

I see, that makes sense. Thank you both for your help, I really appreciate it. Taking mathtext and turning it into a string so the font applies is probably not possible with exponents/symbols as well.

I'm guessing this is more of a feature request to allow mathtext font changes when latex is set to True then, should I close this and make that request?

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

3 participants