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'].

有什么方法可以在 Google Colaboratory 中执行此操作,似乎无法访问此 ttf 文件夹?

例如,我想使用Roboto font。安装后,将使用 mpl.rcParams['font.sans-serif'] = 'Roboto'.

调用

ttf 文件夹在这里:

/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf

所以你想在那里下载 ttf,例如:

!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() 不再有效。这是另一种仍然有效的方法。

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')

matplotlib 3.2 发布后,会更容易。

# For now we must upgrade to 3.2 rc first
# !pip install -U --pre matplotlib  
import matplotlib as mpl
mpl.font_manager.fontManager.addfont('thsarabunnew-webfont.ttf')
mpl.rc('font', family='TH Sarabun New')

想要添加一个目前有效的完整、简洁的答案。

# Download fonts of choice. Here we download Open Sans variants to
# the current directory.
# It's not necessary to download to the share or matplotlib folders:
# /usr/share/fonts/truetype
# /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Regular.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Light.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-SemiBold.ttf'
!wget 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Bold.ttf'
from matplotlib import font_manager as fm, pyplot as plt

# Pick up any fonts in the current directory.
# If you do end up downloading the fonts to /usr/share/fonts/truetype,
# change this to: fm.findSystemFonts()
font_files = fm.findSystemFonts('.')

# Go through and add each to Matplotlib's font cache.
for font_file in font_files:
    fm.fontManager.addfont(font_file)

# Use your new font on all your plots.
plt.rc('font', family='Open Sans')

请注意,有几次这无法正常工作并且没有显示请求的字体(即使没有显示错误或警告)。如果发生这种情况,请尝试将您的 Colab 运行时恢复出厂设置,然后再次 运行。

我最后的解决方案作为参考:

  1. 改变seaborn风格 我强烈推荐这种方法,因为根据 seaborn 设计更改字体系列可能会非常麻烦和不便(许多帖子在 2022/05 年不再适用于我)。因此,如果您只是想摆脱 matplotlib 和 seaborn 中愚蠢的默认字体并且可以使用 Arial,请输入
import matplotlib.style as style 
style.use('seaborn-deep')  
  1. 更改字体类型(从热门答案中借来并自己测试。如果没有按预期运行,请重新启动运行时几次)
import matplotlib as mpl
import matplotlib.font_manager as fm
from matplotlib import font_manager as fm, pyplot as plt

!wget https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf
!wget https://github.com/matomo-org/travis-scripts/blob/master/fonts/Arial.ttf 

font_files = fm.findSystemFonts()

# Go through and add each to Matplotlib's font cache.
for font_file in font_files:
    fm.fontManager.addfont(font_file)

fm.fontManager.ttflist += fm.createFontList(['Times New Roman.ttf'])

# Use your new font on all your plots.
plt.rc('font', family='serif')


t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)

plt.plot(t, s)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.show()