Google Colaboratory matplotlib图表中的自定义字体 [英] Custom fonts in Google Colaboratory matplotlib charts
问题描述
在本地的matplotlib中使用自定义字体需要将.ttf
存储在matplotlib/mpl-data/fonts/ttf/
文件夹中,然后调用mpl.font_manager._rebuild()
,然后设置mpl.rcParams['font.sans-serif']
.
Using custom fonts in matplotlib locally involves storing the .ttf
s in the matplotlib/mpl-data/fonts/ttf/
folder, then calling mpl.font_manager._rebuild()
, then setting mpl.rcParams['font.sans-serif']
.
在Google Colaboratory中,有什么方法似乎无法访问此ttf
文件夹吗?
Is there any way to do this in Google Colaboratory, where it doesn't seem that this ttf
folder is accessible?
例如,我想使用 Roboto字体.安装后,将使用mpl.rcParams['font.sans-serif'] = 'Roboto'
调用.
For example, I'd like to use the Roboto font. After installing, this would be invoked using mpl.rcParams['font.sans-serif'] = 'Roboto'
.
推荐答案
ttf
文件夹在这里:
/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
因此,您想在那里下载ttf
,例如:
So you want to download the ttf
there, e.g.:
!wget https://github.com/Phonbopit/sarabun-webfont/raw/master/fonts/thsarabunnew-webfont.ttf -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
matplotlib.font_manager._rebuild()
matplotlib.rc('font', family='TH Sarabun New')
更新2019-12
_rebuild()
不再起作用.这是另一种仍然有效的方法.
update 2019-12
_rebuild()
no longer works. Here's another method which still works.
import matplotlib
import matplotlib.font_manager as fm
!wget https://github.com/Phonbopit/sarabun-webfont/raw/master/fonts/thsarabunnew-webfont.ttf
fm.fontManager.ttflist += fm.createFontList(['thsarabunnew-webfont.ttf'])
matplotlib.rc('font', family='TH Sarabun New')
这篇关于Google Colaboratory matplotlib图表中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!