Skip to content

ticklabels font using latex #11586

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

Closed
photoniker opened this issue Jul 6, 2018 · 12 comments · Fixed by #28810
Closed

ticklabels font using latex #11586

photoniker opened this issue Jul 6, 2018 · 12 comments · Fixed by #28810
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones! keep Items to be ignored by the “Stale” Github Action topic: text/usetex
Milestone

Comments

@photoniker
Copy link
Contributor

photoniker commented Jul 6, 2018

I can not change the fontstyle of the ticklabels to "sans-serif" when using latex.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)

def xticklabels_example():
    fig = plt.figure() 
    x = np.arange(20)
    y1 = np.cos(x)
    y2 = (x**2)
    y3 = (x**3)
    yn = (y1,y2,y3)
    COLORS = ('b','g','k')
    for i,y in enumerate(yn):
        ax = fig.add_subplot(len(yn),1,i+1)
        ax.plot(x, y, ls='solid', color=COLORS[i]) 
        if i != len(yn) - 1:
            # all but last 
            ax.set_xticklabels( () )
        else:
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14) 
                # specify integer or one of preset strings, e.g.
                #tick.label.set_fontsize('x-small') 
                tick.label.set_rotation('vertical')
    fig.suptitle('Matplotlib xticklabels Example')
    plt.show()

if __name__ == '__main__':
    xticklabels_example()

Matplotlib version

  • Operating system: python 3.6 (itom 3.1.0, www.itom.rocks)
  • Matplotlib version: 2.2.2
  • Python version: 3.6

pltbug2

@pharshalp
Copy link
Contributor

pharshalp commented Jul 8, 2018

this answer by @mdboom might help...
https://stackoverflow.com/a/16345065

And setting the default body text in TeX does not (by default) change the math font. This is (unfortunately standard TeX behavior).

The solution I use when I want all sans-serif out of TeX is to use the cmbright package, which can be turned on by adding:

rc('text.latex', preamble=r'\usepackage{cmbright}')

Using that suggestion, here are the results...

BEFORE
old

AFTER
new

@jklymak
Copy link
Member

jklymak commented Jul 8, 2018

That makes sense to me. OTOH, we maybe could do a better job of documenting this. I don't think its trivial for a user to know that they need to setup TeX by hand this way.

Possibly more info at?

https://matplotlib.org/tutorials/text/usetex.html

@jklymak jklymak added this to the v3.1 milestone Jul 8, 2018
@jklymak jklymak added the Good first issue Open a pull request against these issues if there are no active ones! label Jul 8, 2018
@pharshalp
Copy link
Contributor

I am trying to come up with a good documentation entry for this. Found 3 other issues that have the same root cause (i.e. not knowing that changing text font in TeX doesn't automatically change the math font) -- #8436, #6041, #3999.

@jklymak
Copy link
Member

jklymak commented Jul 10, 2018

I think an entry that says “font selection using tex” or some such would be helpful. Show changing the basic font and the math font.

Long term I really wonder if the Tex rendering should not consult the non-Tex font selection mechanisms at all, and users should set the preamble by hand to get what they want. That would reduce confusion in my mind.

@pharshalp
Copy link
Contributor

pharshalp commented Jul 10, 2018 via email

@ImportanceOfBeingErnest
Copy link
Member

As commented here, one can turn the use of tex off for the ScalarFormatter such that the set font is used for the labels as well.

 ax.xaxis.get_major_formatter()._usetex = False
 ax.yaxis.get_major_formatter()._usetex = False

image

@pharshalp
Copy link
Contributor

pharshalp commented Jul 21, 2018

but then the tick labels won't be rendered using TeX (and other mathematical symbols, numbers will still be using TeX when text.usetex=True) which is inconsistent. When the user sets text.usetex = True the expected result is that all the fonts will be rendered using TeX. Hence, it would be better to ask the user to control the font by using text.latex.preamble.

@ImportanceOfBeingErnest
Copy link
Member

The ticklabels do use tex, because text.usetex=True is set. But they are not rendered in math mode (i.e. the $ are removed). Hence they use the same font used by the rest of the document, but still through the tex system in use.

@pharshalp
Copy link
Contributor

pharshalp commented Jul 21, 2018

ahh right... that makes sense. But, this won't solve the issue if you have a mathematical formula in the title (or annotations) since it has to be rendered in math mode.

usetex_maths_DejaVu-Sans.png
usetex_maths_dejavu-sans

usetex_maths_cmbright.png
usetex_maths_cmbright

@ImportanceOfBeingErnest
Copy link
Member

In this issue, there is no math mode text used. Hence this would solve this issue.

@anntzer
Copy link
Contributor

anntzer commented Nov 14, 2020

In theory I think we could emit something along the lines of \everymath\expandafter{\the\everymath\sf} in the TeX preamble in such cases, which would have the effect of defaulting TeX math to sans-serif.
However, note that the default font.family is already sans-serif (that's why plain text (not math) defaults to a sans-serif font in Matplotlib), so this would effectively affect nearly anyone who is using usetex, even though most users likely associate usetex with the default Computer Modern Roman font (both from classical TeX behavior and from longstanding Matplotlib behavior). So I guess the question is, is it worth making a breaking change here?

@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 27, 2021
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Sep 25, 2021
@QuLogic QuLogic removed this from the v3.6.0 milestone Sep 14, 2022
@QuLogic QuLogic added this to the v3.6.1 milestone Sep 14, 2022
@QuLogic QuLogic modified the milestones: v3.6.1, v3.6.2 Oct 6, 2022
@QuLogic QuLogic modified the milestones: v3.6.2, v3.6.3 Oct 27, 2022
@QuLogic QuLogic modified the milestones: v3.6.3, v3.7.0 Jan 11, 2023
@ksunden ksunden modified the milestones: v3.7.0, v3.7.1 Feb 14, 2023
@QuLogic QuLogic modified the milestones: v3.7.1, v3.7-doc Mar 4, 2023
@QuLogic QuLogic modified the milestones: v3.7-doc, v3.8-doc Sep 9, 2023
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Sep 11, 2024
@anntzer anntzer added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Sep 12, 2024
@QuLogic QuLogic modified the milestones: v3.8-doc, v3.9-doc Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones! keep Items to be ignored by the “Stale” Github Action topic: text/usetex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants