FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Default ScalarFormatter ignores usetex font setting · Issue #8436 · matplotlib/matplotlib · GitHub

Repository navigation

Default ScalarFormatter ignores usetex font setting #8436

Closed as not planned
Closed as not planned

Description

The default ScalarFormatter for axes ignores font setting when used with usetex = True.

Consider the following example, where we use latex and set the font-family to 'sans-serif':

import matplotlib.pyplot as plt 

plt.rc( 'text', usetex=True ) 
plt.rc('font',family = 'sans-serif',  size=20)

fig , ax = plt.subplots(figsize=(5,3))

ax.set_xlabel( r'\textit{x} in a.u.' )
ax.set_ylabel( r'\textit{y} in a.u.' )

plt.tight_layout()
plt.show()

resulting in this image:

As can be seen, the axis labels are in sans-serif font. However, the ticklabels are unexpectedly still in serif font.

The reason seems to be the ScalarFormatter in use. If we change the formatter to e.g. a StrMethodFormatter, we get the expected result:

import matplotlib.pyplot as plt 
import matplotlib.ticker

plt.rc( 'text', usetex=True ) 
plt.rc('font',family = 'sans-serif',  size=20)

fig , ax = plt.subplots(figsize=(5,3))

ax.set_xlabel( r'\textit{x} in a.u.' )
ax.set_ylabel( r'\textit{y} in a.u.' )

fmt = matplotlib.ticker.StrMethodFormatter("{x}")
ax.xaxis.set_major_formatter(fmt)

plt.tight_layout()
plt.show()

Since the use of a ScalarFormatter is desirable for many applications, I think this should be fixed.

Activity

  1. jkrumbiegel commented on May 24, 2017

    I've found the culprit for this behavior, after struggling with the problem myself for quite some time. In the file ticker.py and the class ScalarFormatter, there is a method _set_format(). This method checks if tex is used and formats accordingly:
    if self._usetex:
    self.format = '$%s$' % self.format
    The dollar signs cause the behavior because they mark the tick label numbers as a LaTeX math string. I deleted them and now it works as expected. Or you can delete the whole if clause as it does nothing else.

  2. ImportanceOfBeingErnest commented on Jul 21, 2018

    MemberAuthor

    This means that another possible option would be to turn off the _usetex when using a ScalarFormatter:

     ax.xaxis.get_major_formatter()._usetex = False
    

    Complete example:

    import matplotlib.pyplot as plt 
    
    plt.rc( 'text', usetex=True ) 
    plt.rc('font',family = 'sans-serif',  size=20)
    
    fig , ax = plt.subplots(figsize=(5,3))
    
    ax.set_xlabel( r'\textit{x} in a.u.' )
    ax.set_ylabel( r'\textit{y} in a.u.' )
    
    ax.xaxis.get_major_formatter()._usetex = False
    ax.yaxis.get_major_formatter()._usetex = False
    
    plt.tight_layout()
    plt.show()
    

  3. github-actions commented on Apr 10, 2023

    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!

  4. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL