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

Font family is not set on log-scaled tick labels · Issue #8017 · matplotlib/matplotlib · GitHub

Repository navigation

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

Closed

Description

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

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

Activity

  1. added this to the milestone on Feb 5, 2017
  2. dstansby commented on Feb 5, 2017

    Member

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

  3. tacaswell commented on Feb 5, 2017

    Member

    This is likely something fishy with mathtext...

  4. dstansby commented on Feb 5, 2017

    Member

    Bisects to 6bfd8b1

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

  6. tacaswell commented on Feb 7, 2017

    Member

    what do you mean by 'byte string'?

  7. grahamrow commented on 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.

  8. Tillsten commented on Feb 16, 2017

    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.

  9. Tillsten commented on Feb 16, 2017

    Contributor

    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.

  10. Tillsten commented on Feb 16, 2017

    Contributor

    As a workaround

    import matplotlib.ticker
    matplotlib.ticker._mathdefault = lambda x: '\\mathdefault{%s}'%x
  11. tacaswell commented on Feb 23, 2017

    Member

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

  12. added
    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
    on Feb 23, 2017
  13. self-assigned this
    on Mar 20, 2017
  14. added 2 commits that reference this issue on Mar 20, 2017
    c1d0e41
    eb5950b
  15. added a commit that references this issue on Mar 21, 2017
    5128cf2
  16. NelleV commented on Mar 21, 2017

    Member

    Closed by #8354

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

  18. tacaswell commented on Apr 17, 2017

    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

Metadata

Metadata

Assignees

Labels

Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: text

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL