gpt4 book ai didi

python - 无法在 google colab 中使用 "Times New Roman"字体

转载 作者:行者123 更新时间:2023-12-02 00:13:08 50 4
gpt4 key购买 nike

我正在使用 google colab,我正在尝试使用“Times New Roman”字体进行 matplotlib 绘图。由于 google colab 中的 ttf 文件位于 /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf 所以我所做的是下载 ttf 文件对于来自 https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf 的“Times New Roman”并运行命令 !wget https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf -P/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf 基本上是在google colab的matplotlib的font文件夹下下载了tff文件。

现在,当我尝试使用 matplotlib.font_manager.findfont('Times New Roman') 时,出现如下所示的错误。因此我无法在绘图中使用该字体。

所以我无法找到问题所在,因为我亲自检查并找到了上述字体的 tff 文件。

请帮忙

!wget https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
font_manager.findfont('Times New Roman')
/usr/local/lib/python3.6/dist-packages/matplotlib/font_manager.py:1241: UserWarning: findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.
(prop.get_family(), self.defaultFamily[fontext]))

最佳答案

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']

我认为这会奏效。

关于python - 无法在 google colab 中使用 "Times New Roman"字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58207721/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com