Hi. I am trying to draw a plot using matplotlib.
I set fontfamily
in textprops
to display Chinese characters. I tried 等距更纱黑体 SC
(which is already installed on my computer), but errors occur:
findfont: Font family '等距更纱黑体 SC' not found.
Then I tried Source Han Sans
. It can be found, but the weight is extra light and the style is Japanese style, and I can’t configure them. However, others fonts I installed like Fira Code
can work properly.
So why is this happening, and how to fix this? I suspect that it may be because 等距更纱黑体 SC
and Source Han Sans
are both SuperOTC files, while Fira Code
is just TTF.
Try code as below
cn_font = fm.FontProperties(fname=font_path)
mpl.rcParams['font.family'] = cn_font.get_name()
...
ax.text(fontproperties=cn_font...
Thank you and sorry for the late reply.
Here is my code:
import matplotlib as mpl
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
cn_font = fm.FontProperties(fname=r'C:\Users\EricQiu\AppData\Local\Microsoft\Windows\Fonts\Sarasa-SuperTTC.ttc')
print(cn_font.get_name())
mpl.rcParams['font.family'] = cn_font.get_name()
plt.title('123ABC关复门')
plt.plot([3, 2, 4, 1])
plt.show()
Here is the plot:
Surprisingly, the output is Sarasa Gothic CL
. I also searched on the web and found that matplotlib.font_manager.fontManager.ttflist
stores a list of all avaliable fonts. Sarasa is in that list, however, its name is also Sarasa Gothic CL
.
Sarasa-SuperTTC.ttc
is a collection file of 48 fonts, however, only the first one is detected. Do you know how to make font manager use the rest of the fonts?
Unfortunately, we don’t fully support .ttc
files; only the first font in the collection is used.
If you are able to use mplcairo
as a backend instead, it does support selecting specific fonts from a collection:
Thank you! I will use TTF instead.