Setting the font family through rcParams doesn't impact tick labels if the axis is log-scaled. The size of the font can be changed, but not the family (e.g. using the serif font).
As seen in this screenshot, the linear-scaled y-axis on the left has a serif font while the log-scaled y-axis on the right still has the default sans-serif font. In both cases the font size is set appropriately to 16. It's just the font family that doesn't change. (Pardon the 'shadow' between the two images, that's my Mac, not matplotlib).
Expected outcome
The code should produce tick labels on log-scaled axes with the specified font family.
This isn't just isolated to tick labels. It's anywhere you put in math expressions in a byte string. Add plt.title(r'byte string with $m_at_h$') to the example code above and it will show that the text switches from serif to sans-serif for the rendered math.
I think this has to do with the mathdefault shim for for preserving the classic style. Using just 2.0 defaults the LogFormaterMathtext ignores font.family. After style.use('classic') they are respected.
def_mathdefault(s):
""" For backward compatibility, in classic mode we display sub/superscripted text in a mathdefault block. As of 2.0, the math font already matches the default font, so we don't need to do that anymore. """ifrcParams['_internal.classic_mode']:
return'\\mathdefault{%s}'%selse:
return'{%s}'%s
The assumption of that the math font equals the regular font is not true in general.
I've noticed the workaround suggested doesn't work for cases where tick labels like "2 x 103" appear. These are styled with dejavu sans and there doesn't seem to be anything I can do to change them. They don't seem to use the usual mathtext or even roman
import matplotlib.ticker
from matplotlib import rc
matplotlib.ticker._mathdefault = lambda x: '\\mathdefault{%s}'%x
rc('mathtext', fontset='custom')
rc('mathtext', default='regular')
rc('mathtext', rm='serif')
font_path = 'C:\Windows\Fonts\Roboto-Bold_0.ttf'
font_prop = font_manager.FontProperties(fname=font_path, size=11)
...
for label in (ax.get_xticklabels() + ax.get_yticklabels()):
label.set_fontproperties(font_prop)
plt.xlabel(r"Regular txt, ${math txt, \mathrm{rm math txt}}$", fontproperties=font_prop)
Activity
dstansby commented on Feb 5, 2017
I can reproduce this bug, and it isn't present on 1.5.3
tacaswell commented on Feb 5, 2017
This is likely something fishy with mathtext...
dstansby commented on Feb 5, 2017
Bisects to 6bfd8b1
kbwestfall commented on Feb 7, 2017
This isn't just isolated to tick labels. It's anywhere you put in math expressions in a byte string. Add plt.title(r'byte string with $m_at_h$') to the example code above and it will show that the text switches from serif to sans-serif for the rendered math.
tacaswell commented on Feb 7, 2017
what do you mean by 'byte string'?
grahamrow commented on Feb 10, 2017
In my case this can be solved with:
which will use your specified font for math text, such as the $10^{x}$ being used for logscale tick labels.
Tillsten commented on Feb 16, 2017
I think this has to do with the mathdefault shim for for preserving the classic style. Using just 2.0 defaults the LogFormaterMathtext ignores font.family. After style.use('classic') they are respected.
Tillsten commented on Feb 16, 2017
Actually I think the shim is wrong and uncessary:
The assumption of that the math font equals the regular font is not true in general.
Tillsten commented on Feb 16, 2017
As a workaround
tacaswell commented on Feb 23, 2017
Removing this shim and always using '\\mathdefault' seems like a reasonable change.
BUG: Remove mathtext default shim for ticker (Fixes matplotlib#8017)
BUG: Remove mathtext default shim for ticker (Fixes matplotlib#8017)
Merge pull request #8354 from dopplershift/remove-shim
NelleV commented on Mar 21, 2017
Closed by #8354
binaryfunt commented on Apr 14, 2017
I've noticed the workaround suggested doesn't work for cases where tick labels like "2 x 103" appear. These are styled with dejavu sans and there doesn't seem to be anything I can do to change them. They don't seem to use the usual mathtext or even roman
import matplotlib.ticker from matplotlib import rc matplotlib.ticker._mathdefault = lambda x: '\\mathdefault{%s}'%x rc('mathtext', fontset='custom') rc('mathtext', default='regular') rc('mathtext', rm='serif') font_path = 'C:\Windows\Fonts\Roboto-Bold_0.ttf' font_prop = font_manager.FontProperties(fname=font_path, size=11) ... for label in (ax.get_xticklabels() + ax.get_yticklabels()): label.set_fontproperties(font_prop) plt.xlabel(r"Regular txt, ${math txt, \mathrm{rm math txt}}$", fontproperties=font_prop)tacaswell commented on Apr 17, 2017
@binaryfunt Can you open a new issue for that?