Skip to content
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

[ENH]: Fonts searching logic #28147

Open
ChestnutA opened this issue Apr 28, 2024 · 3 comments
Open

[ENH]: Fonts searching logic #28147

ChestnutA opened this issue Apr 28, 2024 · 3 comments

Comments

@ChestnutA
Copy link

ChestnutA commented Apr 28, 2024

Problem

I'm always frustrated while setting the fonts of text in plots. To begin with, I want the readers to be noticed that I do know a couple of ways to set fonts, and I am here just to give an advise.


I use Windows to be the environment here. The phenomenons are as follows.
The font.sans-serif property in file "matplotlibrc" in the "site-packages\matplotlib\mpl-data" directory is "DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif, simkai". As you can see,

  • there is an item called "simkai" in it,
  • and "simkai.ttf" is located in both
    • site-packages\matplotlib\mpl-data\fonts\ttf
    • C:\Windows\fonts

However, Matplotlib is not able to find the font name, and raise the warning "findfont: Generic family 'sans-serif' not found because none of the following families were found: SimKai".

So, where could the user find and set it? There is a json file within the folder %Userprofile%\.matplotlib. The font "simkai.ttf" is named by "kaiti" in json.

On the contrary, after I set plt.rcParams['font.sans-serif']=['SimSun'], the font "simsun" could be recognized correctly. To be noticed, there are no font files named like "simsun*.ttf" in directories mentioned above. There is only a "C:\Windows\fonts\simsun.ttc".

Similar to what occurred with "kaiti", the way and the only way to recognize "simsun" is to search by the json file under %userprofile%.

I consider this searching mode to be wired and inconvenient.

Proposed solution

Maybe, the logic could be removed from the personal configuration on the system disk. Or add the behavior in User Guide?

@anntzer
Copy link
Contributor

anntzer commented Apr 28, 2024

I believe this font's name (as indicated in the file metadata) is indeed KaiTi; this is also the name under which the font appears e.g. in Word (I can't actually check this right now. Matplotlib uses that same font name -- the name of the file in the filesystem is immaterial.
If you know exactly what font file you want to use you can pass it explicitly using https://matplotlib.org/stable/gallery/text_labels_and_annotations/font_file.html, although indeed right now it is not possible (I think) to include a Path() in rcParams["font.sans-serif"] -- perhaps that would be nice to have.

@ChestnutA
Copy link
Author

I believe this font's name (as indicated in the file metadata) is indeed KaiTi; this is also the name under which the font appears e.g. in Word (I can't actually check this right now. Matplotlib uses that same font name -- the name of the file in the filesystem is immaterial. If you know exactly what font file you want to use you can pass it explicitly using https://matplotlib.org/stable/gallery/text_labels_and_annotations/font_file.html, although indeed right now it is not possible (I think) to include a Path() in rcParams["font.sans-serif"] -- perhaps that would be nice to have.

Thank you, I just don't know to do the set the explicit path every time I use matplotlib. While, actually I am using Chinese Operator System. "Kaiti", it is actually called "楷体" in OS.
So I tried the font names in json file, and they did do correctly in Word. I also has found some uncommon fonts, like "Aptos", "Tinos", which included in Word font selector but couldn't be used in matplotlib.

There is not an explicit map between Chinese font names and their English versions. So I think this feature needs to be improved better.

@anntzer
Copy link
Contributor

anntzer commented May 5, 2024

Ah yes, some fonts are available under multiple names and we currently use only one of them (which may not actually be the OS name). See #4822 (comment).
Anyways, this issue should be left open at least because allowing explicit paths in the rcParams may be a useful workaround too.

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

No branches or pull requests

2 participants