将matplotlib.pyplot.rcparams与未安装的自定义字体一起使用 [英] Use matplotlib.pyplot.rcparams with a custom font which is not installed

查看:355
本文介绍了将matplotlib.pyplot.rcparams与未安装的自定义字体一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用系统中未安装的自定义ttf字体作为matplotlib图中的文本元素.

I'm trying to use a custom ttf font not installed in the system for text element in the matplotlib figure.

with plt.style.context('mplparams.mplstyle'):
    plt.plot(np.sin(np.linspace(0, 3 * np.pi)), '-o')

我知道我可以使用FontManager更改文本属性,但我正在寻找仅涉及外部配置文件的解决方案.

I know I can change the text properties with FontManager but I'm looking for a solution which only involves an external config file.

此刻,我只知道我可以将font.sans-serif更改为字体名称,而不是字体路径.

At the moment I only know that i can change font.sans-serif to a font name, not font path.

这可能吗?

推荐答案

Fontmanager必须知道要使用的字体,否则您将无法进入字体.为了通过rcParams指定一种字体,必须在matplotlib寻找它的文件夹中找到该字体.如果您不想安装任何东西,可以将.ttf文件复制到matplotlib字体文件夹.就我而言,这是

The font to be used has to be known to the Fontmanager, otherwise you cannot get it into the plot. In order to specify a font through rcParams this font must be found in a folder matplotlib would look for it. In case you don't want to install anything, you may copy the .ttf file to the matplotlib font folder. In my case this is

python\Lib\site-packages\matplotlib\mpl-data\fonts

然后,您需要清除font.chache.通过print(matplotlib.get_cachedir())查找其路径并删除fontList文件. (或者,如果需要,请先备份).

Then you need to clear the font.chache. Find out its path via print(matplotlib.get_cachedir()) and delete the fontList files. (Or make a backup first if you like).

然后运行已指定rcParam的脚本

Then run your script which has the rcParam specified

font.sans-serif : <name of font>

或使用

plt.rcParams['font.sans-serif'] = "<name of font>"

另请参见此问题.

这篇关于将matplotlib.pyplot.rcparams与未安装的自定义字体一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆