The first step is to copy the font file to the file directory and delete the Matplolib font cache file
~\site-packages\matplotlib\mpl-data\fonts\ttf\(就是这)
Import matplotlib Print (Matplotlib.matplotlib_fname ()) # The path is displayed ~\site-packages\matplotlib\mpl-data
Other people on the Internet do not copy font files into the directory under Windows, but I must copy them under the Mac, I do not know why.然后就是删除~/.cache/matplotlib/下的文件
The second step is to modify the font_manager.py source file under Matplotlib to fit the *.TTC font file
defget_fontext_synonyms (fontext):"""Return A list of file extensions extensions that is synonyms for the given file extension *fileext*. """ return{'TTF': ('TTF','OTF'),#This line is changed to ' TTF ': (' ttf ', ' OTF ', ' TTC '), 'OTF': ('TTF','OTF'), 'AFM': ('AFM',)}[fontext]
If you use *.ttf fonts, do not change
Step Three
MyFont = Matplotlib.font_manager. Fontproperties (fname='/SYSTEM/LIBRARY/FONTS/PINGFANG.TTC')Print(Myfont.get_name ())#=>pingfang HKmatplotlib.rcparams['Font.sans-serif'] ='Pingfang HK'matplotlib.rcparams['Axes.unicode_minus'] = False#-SymbolsPlt.title ("haha") plt.show ()
PINGFANG.TTC file to be copied into the TTF folder under Matplotlib, the font name and font file name may not be the same, it is better to check first
Finally who knows whether the font file can not be copied into the Mp-data folder?
MacOS under Matplotlib Chinese garbled