Skip to content

Default ScalarFormatter ignores usetex font setting #8436

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
ImportanceOfBeingErnest opened this issue Apr 6, 2017 · 3 comments
Closed

Default ScalarFormatter ignores usetex font setting #8436

ImportanceOfBeingErnest opened this issue Apr 6, 2017 · 3 comments
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: text/usetex topic: text

Comments

@ImportanceOfBeingErnest
Copy link
Member

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:
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()

image

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

@jkrumbiegel
Copy link

jkrumbiegel commented 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.

@ImportanceOfBeingErnest
Copy link
Member Author

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()

image

@github-actions
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 Apr 10, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2023
@rcomer rcomer added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: text/usetex topic: text
Projects
None yet
Development

No branches or pull requests

5 participants