如何配置matplotlib使其能够从本地路径读取字体? [英] How can I configure matplotlib to be able to read fonts from a local path?

查看:208
本文介绍了如何配置matplotlib使其能够从本地路径读取字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将.ttf文件放置在本地文件夹中,并使Matplotlib配置为在该文件夹中查找字体(如果无法在常规系统文件夹中找到它们的话). 上一个答案显示了如何指向任何目录中的特定字体.这是答案中的代码:

I would like to be able to place .ttf files in a local folder and have Matplotlib configured to look in that folder for fonts if it can't find them in the normal system folders. This previous answer showed how to point to a specific font in any directory. Here's the code in the answer:

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

path = '/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS.ttf'
prop = font_manager.FontProperties(fname=path)
mpl.rcParams['font.family'] = prop.get_name()
fig, ax = plt.subplots()
ax.set_title('Text in a cool font', size=40)
plt.show()

问题在于,每次我要在情节中使用Helvetica(或本例中的Comic Sans)时,都必须这样做.我相信另一种解决方案是将ttf文件复制到类似~/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf的文件中,但是我不希望在本地触摸这些东西并将文件放置在本地,这样当我更新matplotlib时它们不会消失,因此更容易同步我的配置跨不同的机器.我觉得应该有一些方法可以在~/.matplotlib/matplotlibrc文件中配置matplotlib,这样,如果我使用Helvetica,则不必每次都提供路径.如何将.ttf文件放置在自定义目录中(或至少一个可以抵御python或matplotlib更新的文件),而不必在每次绘制时都重新键入文件路径?

The problem with this is that I would have to do this every time I want Helvetica (or in this case Comic Sans) in my plot. I believe another solution is to copy the ttf file into something like ~/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf, but I'd prefer not to touch that stuff and place files locally so they don't disappear when I update matplotlib, and so it's easier to sync my configuration across different machines. I feel like there should be some way to configure matplotlib in my ~/.matplotlib/matplotlibrc file so that if I use Helvetica I don't have to provide the path each time. How can I place a .ttf file in a custom directory (or at least one that is safe against python or matplotlib updates) and not have to retype the file path every time I plot?

奖励指出解决方案是否允许我使用相对于import matplotlib; matplotlib.get_configdir()返回的目录的路径,因为对于我的某些计算机而言,~/.config/matplotlib而对于某些计算机而言,则是~/.matplotlib.

Bonus points if the solution allows me to use a path relative to the directory returned by import matplotlib; matplotlib.get_configdir(), since for some of my machines that is ~/.config/matplotlib and for some it's ~/.matplotlib.

推荐答案

万一有人在意,我认为将我的.ttf文件复制到类似~/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf的目录中是最方便的.在更新matplotlib之后,文件仍然存在,因此至少需要一段时间才能重复该过程,这样,我每次绘制时都无需指向目录或调用脚本.如果这样做和/或更改matplotlibrc文件中的默认字体列表(我都做了),则可能必须删除位于~/.matplotlib/fontList.cache或〜/.cache/matplotlib/fontList之类的缓存文件. .cache`. Matplotlib会在您下次绘制某些内容时重新生成.

In case anyone cares, I decided it's most convenient to just copy my .ttf files to the directory that looks something like ~/anaconda/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf. The files were still there after I updated matplotlib, so at least it will probably be a while before I will have to repeat the process, and this way I don't need to point to a directory or call a script every time I plot. If you do this and/or change your default font list in your matplotlibrc file (both of which I did) you'll probably have to delete your cache file located somewhere like ~/.matplotlib/fontList.cache or ~/.cache/matplotlib/fontList.cache`. Matplotlib will regenerate this next time you plot something.

这篇关于如何配置matplotlib使其能够从本地路径读取字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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