Skip to content

[Doc]: Documentation should clearly describe how a user can make matplotlib load custom fonts per default #24872

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

Open
benmaier opened this issue Jan 3, 2023 · 12 comments

Comments

@benmaier
Copy link

benmaier commented Jan 3, 2023

Documentation Link

https://matplotlib.org/stable/users/explain/fonts.html#how-matplotlib-selects-fonts

Problem

The documentation is vague regarding how one should go about letting matplotlib find a directory filled with custom font files.

My current problem is that I want matplotlib, per default, to load a few font files that I cannot install systemwide, because I do not have the rights to do so (linux server). Typically, on Linux, custom fonts are stored either in ~/.fonts/ or ~/.local/share/fonts/. Yet, even after rebuilding the linux font cache, matplotlib does not find any of the custom fonts. Previously, I made that happen by copy-pasting the files to matplotlib's .../mpl-data/ttf/ directory, but that does not seem to work anymore (matplotlib version: 3.6.2). In earlier version there existed a fontList.cache but that does not exist anymore either.

Suggested improvement

There should be an extra section explaining how to set up a custom font-file or font-directory that matplotlib will remember to search when imported. Obviously I'm fine with whatever solution any current matplotlib version has for this problem, but the documentation is lacking clear communication on how to solve it.

@story645
Copy link
Member

story645 commented Jan 3, 2023

Would the work being done in #24866 based off issue #24419 solve this issue?

@benmaier
Copy link
Author

benmaier commented Jan 3, 2023

I don't think so. If I understood #24419 and #24866 correctly, the proposed solution uses font_manager.addFont to add a font file to the manager. I've tried this and the fonts are only added for this particular session, not permanently. Which means that in every script/project I use matplotlib in, I manually have to add the fonts to the font manager using the proposed piece of code. It's not a very sustainable solution. In case there actually is no sustainable solution, I'm happy to open an issue with a feature request. I'm also happy to stand corrected in case I understood sth wrong :)

@tacaswell
Copy link
Member

The cache file that matters is now fontlist-v330.json and

X11FontDirectories = [
# an old standard installation point
"/usr/X11R6/lib/X11/fonts/TTF/",
"/usr/X11/lib/X11/fonts",
# here is the new standard location for fonts
"/usr/share/fonts/",
# documented as a good place to install new fonts
"/usr/local/share/fonts/",
# common application, not really useful
"/usr/lib/openoffice/share/fonts/truetype/",
# user fonts
str((Path(os.environ.get('XDG_DATA_HOME') or _HOME / ".local/share"))
/ "fonts"),
str(_HOME / ".fonts"),
]
is where we build the directories to check for fonts. What you are doing should work.

@benmaier
Copy link
Author

benmaier commented Jan 3, 2023

Curious. Where would this json-file appear? and how do I trigger to rebuild? In earlier versions there was font_manager.rebuild().

@oscargus
Copy link
Member

oscargus commented Jan 4, 2023

It should reside in ~/.matplotlib or similar.

Edit: or whereever $MPLCONFIGDIR may redirect it.

@benmaier
Copy link
Author

benmaier commented Jan 4, 2023

Ok, solved it. The fontlist-v330.json was in ~/.cache/matplotlib/. I deleted it, then it worked. The env $MPLCONFIGDIR was empty, though.

I'd say my initial point stands, though: the documentation does not do a particularly great job at communicating how to solve this particular problem. Since many encountered the same problem in the past, Google is filled with solutions on how to achieve loading custom fonts in older versions of matplotlib, which, in my opinion, makes it all the more important that the issue is addressed in the official documentation for current versions.

@tacaswell
Copy link
Member

I think we need a public "please nuke the font cache and rebuild it" function in matplotlib.font_manager. That would I think solve a lot of these problems (and alleviate the need for users to understand the details of how we cache the locations and meta-data of the fonts).

@jklymak
Copy link
Member

jklymak commented Jan 4, 2023

I don't think it would hurt to document the cache file location as well, and/or provide a public function to expose it. Even if we don't want to support people editing this manually, it may help folks debug their font issues if they can see what is going on.

@tfpf
Copy link
Contributor

tfpf commented Jan 8, 2023

I think the cache file location is returned by matplotlib.get_cachedir(). I remember seeing it on the website.

Edit: found it. https://matplotlib.org/stable/users/faq/troubleshooting_faq.html#matplotlib-configuration-and-cache-directory-locations

@vrx-
Copy link

vrx- commented Mar 11, 2023

Hi. I came across this issue while trying to install a dyslexic-friendly font in my matplotlib. I was following a tutorial I found, but it took me a while to figure out I had to nuke fontlist-v330.json. Anyhow, given that dyslexia is not such an uncommon characteristic in an auditorium and that the word dyslexia doesn't look to appear in matplotlib documentation... I thought of, perhaps either:

  • including an example using this particular font in this bit of documentation, or
  • including a dyslexic-friendly font in matptlolib's standard font library.
    The first will be easier I think, but the second may have more impact, though I have no clue how feasible that would be.
    Any thoughts on this?

@story645
Copy link
Member

I'm more inclined towards the first because we don't have the expertise to be an authority on what makes for a good dyslexic-friendly font, but I think that's a great motivation for using this feature and so would be all for including it in an example.

@saranti
Copy link
Contributor

saranti commented Jun 16, 2023

I'm going to send a PR for this so just to be clear, the work here is:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants