Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Default ScalarFormatter ignores usetex font setting #8436

Closed as not planned
Closed as not planned
@ImportanceOfBeingErnest

Description

@ImportanceOfBeingErnest

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.

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