Custom fonts in Google Colaboratory matplotlib charts

可紊 提交于 2019-12-12 18:08:33

问题


Using custom fonts in matplotlib locally involves storing the .ttfs in the matplotlib/mpl-data/fonts/ttf/ folder, then calling mpl.font_manager._rebuild(), then setting mpl.rcParams['font.sans-serif'].

Is there any way to do this in Google Colaboratory, where it doesn't seem that this ttf folder is accessible?

For example, I'd like to use the Roboto font. After installing, this would be invoked using mpl.rcParams['font.sans-serif'] = 'Roboto'.


回答1:


The ttf folder is here:

/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/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')


来源:https://stackoverflow.com/questions/51810908/custom-fonts-in-google-colaboratory-matplotlib-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!