Skip to content

[Doc]: add how to know available fonts... #21761

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

Closed
jklymak opened this issue Nov 26, 2021 · 14 comments · Fixed by #21799
Closed

[Doc]: add how to know available fonts... #21761

jklymak opened this issue Nov 26, 2021 · 14 comments · Fixed by #21799
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones!
Milestone

Comments

@jklymak
Copy link
Member

jklymak commented Nov 26, 2021

Documentation Link

https://matplotlib.org/stable/tutorials/text/text_props.html#default-font

Problem

In https://matplotlib.org/stable/tutorials/text/text_props.html#default-font we are told that we can use any "available fonts" for fontfamily. It would be nice if this tutorial, or another, indicated how to get that list of available fonts, and then specfically how to specify them in fontfamily. I tried a couple of things on stack overflow, but font family does not appear to accept full strings like "Times New Roman", so I assume I need to specify something more low level?

Overall I find font handling still quite confusing, and it seems it requires deeper knwoledge than most users are going to have (or its explained somewhere that is not adequately cross linked).

Suggested improvement

No response

Matplotlib Version

main

@jklymak
Copy link
Member Author

jklymak commented Nov 26, 2021

I'll ping @aitikgupta here because I think he knows the font handling pretty well, and maybe could suggest an in-Matplotlib way of getting the right info (obviously we can't specify for all OS-es where the fonts are stored).

@tfpf
Copy link
Contributor

tfpf commented Nov 26, 2021

font family does not appear to accept full strings like "Times New Roman"

For me, on WSL, the RC parameter font.family accepts 'Times New Roman', or the name of any installed font, in a case-insensitive manner.

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'mV bOlI'
plt.figure().text(0.1, 0.5, 'This is MV Boli', size=35)
plt.show()

image

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'tImeS nEw ROmaN'
plt.figure().text(0.1, 0.5, 'This is Times New Roman', size=35)
plt.show()

image

Maybe 'available font' means 'installed font'?

@jklymak
Copy link
Member Author

jklymak commented Nov 26, 2021

Ah, OK, fair enough. Still the main point is that the list of possible fonts is not readily accessible.

@anntzer
Copy link
Contributor

anntzer commented Nov 26, 2021

The available names are the .name attributes of the entries in matplotlib.font_manager.fontManager.ttflist. Agreed this could be 1) better documented and 2) more easily accessible.

@jklymak
Copy link
Member Author

jklymak commented Nov 26, 2021

Perfect, thanks! I actually think a little wrapper around that would help people (me). Even just matplotlib.show_available_fontnames (if the top level is where we keep this sort of thing). I'll mark as a good first issue because I think this would be relatively straightforward to add and document.

@jklymak jklymak added the Good first issue Open a pull request against these issues if there are no active ones! label Nov 26, 2021
@ojeda-e
Copy link
Contributor

ojeda-e commented Nov 26, 2021

Can I work on this as a good first issue over the weekend? :)

The suggestion by @jklymak at the top level makes sense. Maybe another option to consider is matplotlib.font_manager.font_names?

Thanks

@tacaswell
Copy link
Member

@ojeda-e Please have at it!

If you think there are a couple of reasonable ways to implement this, it would make it much easier to review if you include a discussion of why you picked the implementation you did in the comments on the PR.

@ojeda-e
Copy link
Contributor

ojeda-e commented Nov 26, 2021

Thanks @tacaswell!
Sounds good, I'll make sure a clear discussion is added.

@jklymak
Copy link
Member Author

jklymak commented Nov 26, 2021

I also don't know that it is a problem to have a low-level access and an upper-level access that wraps it. There are a few things that are nice to have near top level so folks don't have to try to remember the name of the library where they are implemented (the balance being everything can't be top level!)

@aitikgupta
Copy link
Contributor

Maybe 'available font' means 'installed font'?

Even after installing a font it doesn't end up in Matplotlib's font cache and in turn, isn't available; one would need to rebuild it.
(note: rebuild_if_missing is supposed to rebuild it if a font family isn't found)

There existed a way earlier (it wasn't a public function, but the top results of "matplotlib rebuild font cache" on Google still show this old website and SOs whose old answers mention _rebuild())

I generally do this another way programmatically:

from pathlib import Path
import matplotlib as mpl
import matplotlib.font_manager as fm

fm_path = Path(mpl.get_cachedir(), f"fontlist-v{fm.FontManager.__version__}.json")
fm = fm.FontManager()
fm.json_dump(fm, fm_path)

@jklymak
Copy link
Member Author

jklymak commented Nov 28, 2021

So how do you "install" an "available" font?

@aitikgupta
Copy link
Contributor

So how do you "install" an "available" font?

We "install" a font natively (a double click on a TTF/OTF file will bring up the installation) - the utility is provided by the OS, and the font is installed globally in the system.

And once it is "installed", it can be made "available" for Matplotlib after rebuilding font cache.

@jklymak
Copy link
Member Author

jklymak commented Nov 28, 2021

OK, so you are saying that just because the user does a system install of a font doesn't necessarily mean Matplotlib will consider it available, because it has to rebuild the cache first.

@aitikgupta
Copy link
Contributor

OK, so you are saying that just because the user does a system install of a font doesn't necessarily mean Matplotlib will consider it available, because it has to rebuild the cache first.

yep!

@QuLogic QuLogic added this to the v3.6.0 milestone Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Good first issue Open a pull request against these issues if there are no active ones!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants