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

Skip to content

Font family is not set on log-scaled tick labels #8017

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
michael-a-hansen opened this issue Feb 4, 2017 · 13 comments
Closed

Font family is not set on log-scaled tick labels #8017

michael-a-hansen opened this issue Feb 4, 2017 · 13 comments
Assignees
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. topic: text
Milestone

Comments

@michael-a-hansen
Copy link

Bug report

Bug summary

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

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib import rcParams

rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16

x = [0, 1, 2]
y = [1, 100, 10000]

plt.plot(x, y)
plt.yscale('log')
plt.savefig('logscaled.png')
plt.clf()

plt.plot(x, y)
plt.savefig('not-logscaled.png')

Actual outcome

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

issue

Expected outcome

The code should produce tick labels on log-scaled axes with the specified font family.

Matplotlib version

  • Mac OS X 10.10.5
  • python 3.5, installed from a Mac installer pkg
  • matplotlib 2.0.0 - upgraded February 3 with python3 -m pip install --upgrade matplotlib
@dstansby dstansby added this to the 2.0.1 (next bug fix release) milestone Feb 5, 2017
@dstansby
Copy link
Member

dstansby commented Feb 5, 2017

I can reproduce this bug, and it isn't present on 1.5.3

@tacaswell
Copy link
Member

This is likely something fishy with mathtext...

@dstansby
Copy link
Member

dstansby commented Feb 5, 2017

Bisects to 6bfd8b1

@kbwestfall
Copy link

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
Copy link
Member

what do you mean by 'byte string'?

@grahamrow
Copy link

grahamrow commented Feb 10, 2017

In my case this can be solved with:

from matplotlib import rc
rc('mathtext', default='regular') 

which will use your specified font for math text, such as the $10^{x}$ being used for logscale tick labels.

@Tillsten
Copy link
Contributor

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
Copy link
Contributor

Tillsten commented Feb 16, 2017

Actually I think the shim is wrong and uncessary:

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.
    """
    if rcParams['_internal.classic_mode']:
        return '\\mathdefault{%s}' % s
    else:
        return '{%s}' % s

The assumption of that the math font equals the regular font is not true in general.

@Tillsten
Copy link
Contributor

Tillsten commented Feb 16, 2017

As a workaround

import matplotlib.ticker
matplotlib.ticker._mathdefault = lambda x: '\\mathdefault{%s}'%x

@tacaswell
Copy link
Member

Removing this shim and always using '\\mathdefault' seems like a reasonable change.

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Feb 23, 2017
@dopplershift dopplershift self-assigned this Mar 20, 2017
dopplershift added a commit to dopplershift/matplotlib that referenced this issue Mar 20, 2017
The shim was put in place because the thinking was changing the default
fonts would eliminate the need. That's not the case, as that prevents
other font changes from being reflected when mathtext is used.
dopplershift added a commit to dopplershift/matplotlib that referenced this issue Mar 20, 2017
The shim was put in place because the thinking was changing the default
fonts would eliminate the need. That's not the case, as that prevents
other font changes from being reflected when mathtext is used.
NelleV added a commit that referenced this issue Mar 21, 2017
BUG: Remove mathtext default shim for ticker (Fixes #8017)
@NelleV
Copy link
Member

NelleV commented Mar 21, 2017

Closed by #8354

@NelleV NelleV closed this as completed Mar 21, 2017
@binaryfunt
Copy link

binaryfunt commented 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)

figure_56

@tacaswell
Copy link
Member

@binaryfunt Can you open a new issue for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. topic: text
Projects
None yet
Development

No branches or pull requests

9 participants