Skip to content

.set_xscale('log') uses missing 'Unicode minus' (ignoring rcParams['axes.unicode_minus'] and rcParams['mathtext.fallback_to_cm']) #12039

@Bashky

Description

@Bashky

Bug report

Bug summary

When using text in \mathdefault mode, the hyphen character '-' is remapped to Unicode minus (0x2212). When the font does not have this character the currency symbol is shown as a placeholder, incorrectly ignoring the settings rcParams['axes.unicode_minus'] and rcParams['mathtext.fallback_to_cm']. This is triggered by .set_xscale('log') which uses LogFormatterSciNotation.

Code for reproduction

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['Papyrus'] # Or any other font without U+2212.
plt.rcParams['axes.unicode_minus'] = False # Will fix this y-axis, but not title or x-axis
assert plt.rcParams['mathtext.fallback_to_cm'] # Should be True by default.
plt.rcParams['figure.dpi'] = 150 # Just to see better.

plt.title(r'$\mathdefault{-5}$') # Will show currency symbol for -.

# This also happens on log-scale (which uses LogFormatterSciNotation).
plt.plot([1e-4, 10], [-10, 10])
plt.gca().set_xscale('log')

plt.show()

If you don't have Papyrus, you can try https://www.exljbris.com/delicious.html.

Actual outcome

...\lib\site-packages\matplotlib\mathtext.py:849: MathTextWarning: Font 'default' does not have a glyph for '-' [U+2212]
  MathTextWarning)
...\lib\site-packages\matplotlib\mathtext.py:850: MathTextWarning: Substituting with a dummy symbol.
  warn("Substituting with a dummy symbol.", MathTextWarning)

image

The y-axis (which is linear, uses ScalarFormatter by default) shows the non-unicode '-' because of rcParams['axes.unicode_minus'], this is expected behavior.
But the title and x-axis (which uses the LogFormatterSciNotation formatter, which uses \mathdefault, presumably for the superscript) show currency symbol instead of minus.

Expected outcome

I expect:

  1. .set_xscale('log') to adhere to rcParams['axes.unicode_minus'] and use the hyphen '-'
  2. If it doesn't, at least listen to rcParams['mathtext.fallback_to_cm'] to fallback to Computer Modern instead of the currency symbol
  3. (If it doesn't, the '-' or even en-dash '–' would be better fallbacks than the currency symbol in this case)

It is unclear to me why https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/mathtext.py#L831 doesn't trigger the fallback_to_cm functionality. Perhaps it somehow doesn't realize that the math-font in \mathdefault is a custom font.

Nasty workaround

try:
    old_get_unicode_index
except NameError:
    print 'Patching matplotlib.mathtext.get_unicode_index'
    import matplotlib.mathtext as mathtext
    old_get_unicode_index = mathtext.get_unicode_index
    mathtext.get_unicode_index = lambda symbol, math=True:\
        ord('-') if symbol == '-' else old_get_unicode_index(symbol, math)

Matplotlib version

  • Operating system: Windows 10 [Version 10.0.16299.611]
  • Matplotlib version: 2.2.3
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
  • Jupyter version: 5.4.0

I've installed matplotlib through conda: conda update matplotlib.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions