如何使用 matplotlib 全局字体的自定义路径设置自定义字体? [英] how to set up a custom font with custom path to matplotlib global font?
本文介绍了如何使用 matplotlib 全局字体的自定义路径设置自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用中有一个自定义字体
app_path='/home/user1/myapp'
fname='/home/user1/myapp/font/myfont.ttf'
要为 matplotlib 设置 globlal 字体,文档是这样说的:
To setup globlal font to matplotlib,the docs said like this:
plt.rcParams['font.sans-serif']=['xxx font']
但它仅在系统字体路径中已有字体时才有效,并且我必须在我的应用程序路径中使用我的自定义字体 '/home/user1/myapp/font/myfont.ttf'
But it only works when the font already in system font path,and I have to use my custom font in my app path '/home/user1/myapp/font/myfont.ttf'
我知道有一种方法:
fname='/home/user1/myapp/font/myfont.ttf'
myfont=fm.FontProperties(fname=fname)
ax1.set_title('title test',fontproperties=myfont)
但这不是我想要的,我不想一直设置'fontproperties',因为有很多代码要更改
But that is not what I want,I don't want to set 'fontproperties' all the time,because there are some much code to change
推荐答案
解决了这个问题:
import matplotlib.font_manager as font_manager
font_dirs = ['/my/custom/font/dir', ]
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
mpl.rcParams['font.family'] = 'My Custom Font'
fontpaths
kwarg 也可以是字符串,以防您只有一个目录可供导入.
The fontpaths
kwarg can also be a string in case you only have a single directory to import from.
这篇关于如何使用 matplotlib 全局字体的自定义路径设置自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文