Visitar URL original
[Bug]: Font lookup when rendering math text · Issue #32164 · matplotlib/matplotlib · GitHub
Skip to content

[Bug]: Font lookup when rendering math text #32164

Description

@dnicolodi

Bug summary

With this in matplotlibrc:

font.sans-serif : Ubuntu
font.size : 10.0
font.weight: 300
mathtext.default : regular

I expect all text (math and regular) to be rendered with the "Ubuntu" font at a size of 10.0 and a weight of 300. This is what happens. However, when I use math text, I get this warning multiple times:

findfont: Failed to find font weight 300, now using 400.

It seems that the math text rendering machinery looks up the glyphs for the math text in a different font before looking them up in the desired font.

It would be great if the warning could specify which font is being looked up.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib

matplotlib.rcParams.update({
    'font.sans-serif': 'Ubuntu',
    'font.size': 10.0,
    'font.weight': 300,
    'mathtext.default': 'regular',
})

plt.figure()
plt.text(0.1, 0.5, 'Foo')
plt.text(0.2, 0.5, 'Foo $x$')
plt.show()

Actual outcome

$ python mathtext.py
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.
findfont: Failed to find font weight 300, now using 400.

Expected outcome

No warnings.

Additional information

No response

Operating system

Windows 11

Matplotlib Version

3.11.0

Matplotlib Backend

qtagg

Python version

3.14.6

Jupyter version

No response

Installation

pixi

Activity

  1. subhakartadimalla commented on Aug 3, 2026

    @subhakartadimalla

    Hi! I'd like to work on this issue. Could you please assign it to me?

  2. QuLogic commented on Aug 3, 2026

    @QuLogic
    Member

    There is already an existing PR for this issue.

  3. added this to the v3.11.2 milestone on Aug 3, 2026
  4. dnicolodi commented on Aug 3, 2026

    @dnicolodi
    ContributorAuthor

    Thanks @QuLogic. #32104 addresses part of this issue. It does not address the fact that a font that is not the one that matplotlib tries to find a font that is not the one that will be used to render the text.

  5. QuLogic commented on Sep 4, 2026

    @QuLogic
    Member

    So the handling of mathtext.default is a bit strange; I can confirm that it does do what it says, but I can't figure out that path to it, since the rcParam is read in very few places. For now, I'll unlink the PR to keep this open, and will continue investigating later.

  6. QuLogic commented on Sep 5, 2026

    @QuLogic
    Member

    OK, now that we have the font family in the warning, it's a bit clearer where this is coming from. The changed font doesn't really matter; it's just the font.weight: 300 that does it:

    findfont: Failed to find font weight 300 for DejaVu Sans, now using 400.
    findfont: Failed to find font weight 300 for DejaVu Sans, now using 400.
    findfont: Failed to find font weight 300 for STIXGeneral, now using 400.
    findfont: Failed to find font weight 300 for STIXGeneral, now using 400.
    findfont: Failed to find font weight 300 for STIXNonUnicode, now using 400.
    findfont: Failed to find font weight 300 for STIXNonUnicode, now using 400.
    findfont: Failed to find font weight 300 for STIXSizeOneSym, now using 400.
    findfont: Failed to find font weight 300 for STIXSizeTwoSym, now using 400.
    findfont: Failed to find font weight 300 for STIXSizeThreeSym, now using 400.
    findfont: Failed to find font weight 300 for STIXSizeFourSym, now using 400.
    findfont: Failed to find font weight 300 for STIXSizeFiveSym, now using 400.
    findfont: Failed to find font weight 300 for cmmi10, now using 400.
    findfont: Failed to find font weight 300 for cmsy10, now using 400.
    findfont: Failed to find font weight 300 for cmr10, now using 400.
    findfont: Failed to find font weight 300 for cmtt10, now using 400.
    findfont: Failed to find font weight 300 for cmti10, now using 400.
    findfont: Failed to find font weight 300 for cmb10, now using 400.
    findfont: Failed to find font weight 300 for cmss10, now using 400.
    findfont: Failed to find font weight 300 for cmex10, now using 400.
    findfont: Failed to find font weight 300 for DejaVu Sans, now using 400.
    findfont: Failed to find font weight 300 for DejaVu Sans, now using 400.
    findfont: Failed to find font weight 300 for DejaVu Sans Mono, now using 400.
    findfont: Failed to find font weight 300 for DejaVu Sans Display, now using 400.
    

    This comes from the initialization of DejaVuFonts:

    def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlags):
    # This must come first so the backend's owner is set correctly
    if isinstance(self, DejaVuSerifFonts):
    self._fallback_font = StixFonts(default_font_prop, load_glyph_flags)
    else:
    self._fallback_font = StixSansFonts(default_font_prop, load_glyph_flags)
    self.bakoma = BakomaFonts(default_font_prop, load_glyph_flags)
    TruetypeFonts.__init__(self, default_font_prop, load_glyph_flags)
    # Include Stix sized alternatives for glyphs
    self._fontmap.update({
    '1': 'STIXSizeOneSym',
    '2': 'STIXSizeTwoSym',
    '3': 'STIXSizeThreeSym',
    '4': 'STIXSizeFourSym',
    '5': 'STIXSizeFiveSym',
    })
    for key, name in self._fontmap.items():
    fullpath = findfont(name)
    self.fontmap[key] = fullpath
    self.fontmap[name] = fullpath

    which is created because of the default mathtext.fontset. This could be set to some other fontset, but cannot really be set to a custom font (despite the custom option) without #31064.

    The problem in all those cases is that we request our bundled fonts by family name only. This allows the defaults from rcParams to slip through and then the lookup searches for it in 300 weight, which doesn't exist. The easiest fix should be to force the font weight, at least until the full Unicode math font support is added.

  7. modified the milestones: v3.11.2, v3.11.3 on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions