Bug summary
The '\times' glyph in minor ticklabels of log plots is not being recognized because it is getting wrapped inside '\mathdefault{...}'. The problem and a temporary solution (simply removing '\mathdefault') is fully described on Stack Overflow.
Code for reproduction
importmatplotlibimportmatplotlib.pyplotasplt# Customize matplotlibmatplotlib.rcParams.update({# Use mathtext, not LaTeX'text.usetex': False,
# Use the Computer modern font'font.family': 'serif',
'font.serif': 'cmr10',
'mathtext.fontset': 'cm',
# Use ASCII minus'axes.unicode_minus': False,
})
# Plotplt.semilogy([-0.03, 0.05], [0.3, 0.05])
plt.savefig('test.png')
Actual outcome
python/lib/python3.6/site-packages/matplotlib/mathtext.py:866: MathTextWarning: Font 'default' does not have a glyph for '\times' [U+d7]
MathTextWarning)
python/lib/python3.6/site-packages/matplotlib/mathtext.py:867: MathTextWarning: Substituting with a dummy symbol.
warn("Substituting with a dummy symbol.", MathTextWarning)
Expected outcome
This is only a problem with matplotlib 2.x, as 1.x never automatically places minor ticklabels.
Matplotlib version
Operating system: Linux
Matplotlib version: 2.0.2, but the bug is consistently reproduced in all of matplotlib 2.x
Matplotlib backend: Agg, TkAgg
Python version: 3.6
I have installed Python and matplotlib from source.
@jklymak Not an actual one. I have an ugly workaround, as demonstrated on StackOverflow (see link above). Reading my answer there, the underlying problem should be apparent. I think it won't be hard to fix for one with knowledge of the matpltolib.mathtext internals.
A simple fix would be what @anntzer mentioned, i.e. similar to #18397
A long term fix would be define fallback for mathtext such that the process of finding symbols:
where symbols (e.g. minus) and letters/digits are in different font files (cmr10/cmsy10);
would be automated. I can take this up during the GSoC period once the fallback for non-mathtext is implemented, for now I guess it'd be better to go with the simple fix.
Long term I would like to get rid of the numerous cmXX10 fonts and replace them by the monolithic Latin Modern Math font, which should avoid all these problems.
It is since a few days possible to use font fallback, i.e., when a glyph is not found in the requested font, the next font in the list is tried.
I guess that there should be a solution using this, but based on my tests (and not really knowing much about the currently introduced change), it may be that this is not implemented for mathtext.
diff --git i/lib/matplotlib/_mathtext.py w/lib/matplotlib/_mathtext.py
index 0aa7bec574..53c2eac216 100644
--- i/lib/matplotlib/_mathtext.py+++ w/lib/matplotlib/_mathtext.py@@ -462,11 +462,15 @@ class UnicodeFonts(TruetypeFonts):
found_symbol = False
font = self._get_font(new_fontname)
if font is not None:
- if font.family_name == "cmr10" and uniindex == 0x2212:- # minus sign exists in cmsy10 (not cmr10)- font = get_font(- cbook._get_data_path("fonts/ttf/cmsy10.ttf"))- uniindex = 0xa1+ if font.family_name == "cmr10":+ if uniindex == 0xd7: # times sign from cmsy10, not cmr10+ font = get_font(+ cbook._get_data_path("fonts/ttf/cmsy10.ttf"))+ uniindex = 0xa3+ if uniindex == 0x2212: # minus sign from cmsy10, not cmr10+ font = get_font(+ cbook._get_data_path("fonts/ttf/cmsy10.ttf"))+ uniindex = 0xa1
glyphindex = font.get_char_index(uniindex)
if glyphindex != 0:
found_symbol = True
Discussing this at the dev call today, it seems like implementing font fallback for mathtext is not really feasible. So something like the patch above is the way to go. Are you interested in picking this up @tfpf ?
If I understand correctly, this is necessary because font fallback in regular text was implemented by #20740? (I think that should be a separate pull request, though?)
This is unrelated to regular font fallback. mathtext has "always" supported font fallback using its own code (because it was necessary from the very beginning), but a large amount of complexity is that the font files we use for math have irregular encodings that basically need to be hardcoded.
It is still not completely clear to me why it should be reverted.:sweat_smile: If minus signs are not present in that font, and are being substituted in as a result of Mathtext's fallback, it looks like that warning is required?
UserWarning: cmr10 font should ideally be used with mathtext, set axes.formatter.use_mathtext to True
UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.
and fails to render minus signs on the horizontal axis. (Setting use_mathtext to True enables the Mathtext fallback for minus signs; setting family to ['cmr10', 'STIXGeneral'] triggers regular font fallback. Either of these two actions results in the minus signs being rendered correctly.)
So I believe the warning is genuine and should not be removed?
I guess it's no big deal either way, the warning can also be left until we actually switch to a full latin modern font with all glyphs in a single font file.
Activity
jmd-dk commented on Apr 14, 2021
May I please bump this? More than 3 years down the line, this is still a problem in Matplotlib 3.4.1.
jklymak commented on Apr 14, 2021
Seems reasonable. Did you have a fix?
anntzer commented on Apr 14, 2021
My guess is that the solution would be similar to #17007 (comment) / #18397.
jmd-dk commented on Apr 14, 2021
@jklymak Not an actual one. I have an ugly workaround, as demonstrated on StackOverflow (see link above). Reading my answer there, the underlying problem should be apparent. I think it won't be hard to fix for one with knowledge of the matpltolib.mathtext internals.
anntzer commented on Apr 14, 2021
@aitikgupta Perhaps this and the issues linked in my previous message may be of interest for you.
aitikgupta commented on Apr 15, 2021
A simple fix would be what @anntzer mentioned, i.e. similar to #18397
A long term fix would be define fallback for mathtext such that the process of finding symbols:
would be automated. I can take this up during the GSoC period once the fallback for non-mathtext is implemented, for now I guess it'd be better to go with the simple fix.
Or an even longer term fix would again be what @anntzer mentioned in #17007 (comment):
tfpf commented on Aug 15, 2022
This is still not fixed on the current main branch. Is there any progress on the long-term fix?
oscargus commented on Aug 15, 2022
It is since a few days possible to use font fallback, i.e., when a glyph is not found in the requested font, the next font in the list is tried.
I guess that there should be a solution using this, but based on my tests (and not really knowing much about the currently introduced change), it may be that this is not implemented for mathtext.
tfpf commented on Aug 16, 2022
Is it worth considering the short-term fix, then? Since this bug is quite old.
anntzer commented on Aug 18, 2022
The following basically implements #10029 (comment), I think:
#20235 should also be reverted with that patch.
oscargus commented on Aug 18, 2022
Discussing this at the dev call today, it seems like implementing font fallback for mathtext is not really feasible. So something like the patch above is the way to go. Are you interested in picking this up @tfpf ?
tfpf commented on Aug 20, 2022
Sure, will do.
If I understand correctly, this is necessary because font fallback in regular text was implemented by #20740? (I think that should be a separate pull request, though?)
anntzer commented on Aug 20, 2022
This is unrelated to regular font fallback. mathtext has "always" supported font fallback using its own code (because it was necessary from the very beginning), but a large amount of complexity is that the font files we use for math have irregular encodings that basically need to be hardcoded.
tfpf commented on Aug 21, 2022
It is still not completely clear to me why it should be reverted.:sweat_smile: If minus signs are not present in that font, and are being substituted in as a result of Mathtext's fallback, it looks like that warning is required?
import matplotlib.pyplot as plt plt.rcdefaults() plt.rc('font', family=['cmr10']) plt.rc('mathtext', fontset='cm') plt.rc('axes.formatter', use_mathtext=False) plt.semilogy([-0.03, 0.05], [0.3, 0.05]) plt.show()This throws
UserWarning: cmr10 font should ideally be used with mathtext, set axes.formatter.use_mathtext to True UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.and fails to render minus signs on the horizontal axis. (Setting use_mathtext to True enables the Mathtext fallback for minus signs; setting family to ['cmr10', 'STIXGeneral'] triggers regular font fallback. Either of these two actions results in the minus signs being rendered correctly.)
So I believe the warning is genuine and should not be removed?
anntzer commented on Aug 21, 2022
I guess it's no big deal either way, the warning can also be left until we actually switch to a full latin modern font with all glyphs in a single font file.