-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[Feature Request] Better default font family that supports all unicode chars #20251
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
Comments
Does https://matplotlib.org/stable/gallery/text_labels_and_annotations/font_family_rc_sgskip.html help? This SO post is also quite good: https://stackoverflow.com/questions/45877746/changing-fonts-in-matplotlib Google doesn't turn up more complete docs on our side, which seems a shame. Are there more @aitikgupta ? |
@jklymak Thanks for the reply!
I set from matplotlib import rcParams
rcParams['font.family'] = 'Hack Nerd Font'
rcParams['font.sans-serif'] = ['Hack Nerd Font']
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], label='test')
ax.legend()
ax.set_xlabel('测试')
plt.show() MPL didn't complain about missing glyphs, but the characters are not rendered correctly.
I was able to get a list of fonts files (font_list) installed on my machine, but the last line of the code
didn't work. |
If there are missing glyphs (text not rendering correctly) - Matplotlib is supposed to emit out the warnings, you might want to try it again? Are you certain that the font you're using (Hack Nerd Font) contains these glyphs - You can follow this blog to use custom fonts with Pandas, another SO discussion you might want to take a look at. Basically there's 2 ways:
|
Unfortunately, we don't have a solid entry point which talks in-depth about using custom fonts - we might be tracking this here: #5941 I'd vote for closing this issue in favour of other similar issues (eg, #17498, #18883) that basically track the proposed solutions mentioned by @tddschn:
|
Sure let's continue discussion at those issues. Thanks! |
I wonder if there is a MPL predefined font (i.e. non-custom font) that has good unicode support? |
Nerd Fonts simply adds a bunch of fancy symbols, so we can ignore that part of it. Going to the Hack fonts (https://sourcefoundry.org/hack/) details, they state:
That is nowhere near enough glyphs to cover CJK, and if you check it in e.g., fontforge, you will find no CJK glyphs. Your terminal supports glyph fallback and is picking a different font, as is discussed in the other issues. |
@QuLogic You're right. I found a working solution but it's ugly (PingFang is a system font that supports CJK chars shipped with macOS): |
Those are great ideas! |
Oh good find. I knew we had something like that, but it's not easy to find. |
@thedirk Yes, see https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#font-fallback that lets you set up multiple fonts and we will fallback through them until we find the glyph requested. |
Problem
I'm working with a dataset that contains lots of east Asian characters. When trying to plot the data, I got a flood of warnings like this:
RuntimeWarning: Glyph <number> missing from current font. font.set_text(s, 0.0, flags=flags)
, and the generated plot had no readable texts.Since I'm new to matplotlib and was calling
plot
from a pandasDataFrame
, I couldn't find a trivial way to get the chars displayed correctly. Some suggest downloading a font file and configuring the font family with the path to that file, all seems quite complicated and I couldn't figure how to do that directly fromDataFrame.plot
arguments.A few quick searches lead me to this issue from May 2019: #14269 (comment)
I don't think the current default font is a sensible choice since it only support latin characters.
Proposed Solution
Additional context and prior art
Warning screenshot:

Plot generated:

matplotlib version: 3.4.2 (from conda-forge)
OS: macOS 11.3.1
The text was updated successfully, but these errors were encountered: