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).
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).
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()
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()
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.
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.
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.
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!)
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())
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.
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.
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.
Activity
jklymak commented on 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 commented on Nov 26, 2021
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.
Maybe 'available font' means 'installed font'?
jklymak commented on Nov 26, 2021
Ah, OK, fair enough. Still the main point is that the list of possible fonts is not readily accessible.
anntzer commented on 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 commented on 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.
ojeda-e commented on 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 commented on Nov 26, 2021
@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 commented on Nov 26, 2021
Thanks @tacaswell!
Sounds good, I'll make sure a clear discussion is added.
jklymak commented on 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 commented on Nov 27, 2021
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:
jklymak commented on Nov 28, 2021
So how do you "install" an "available" font?
aitikgupta commented on Nov 28, 2021
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 commented on 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 commented on Nov 28, 2021
yep!