【问题标题】:Not able to used "Times New Roman" font in google colab无法在 google colab 中使用“Times New Roman”字体
【发布时间】:2020-02-01 02:23:05
【问题描述】:

我正在使用 google colab,并且正在尝试使用“Times New Roman”字体进行 matplotlib 绘图。由于 google colab 中的 ttf 文件位于/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf 所以我所做的是从https://github.com/trishume/OpenTuringCompiler/blob/master/stdlib-sfml/fonts/Times%20New%20Roman.ttf 下载“Times New Roman”的 ttf 文件并运行命令!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 基本上将 tff 文件下载到字体文件夹中谷歌 colab 中的 matplotlib。

在此之后,当我尝试使用 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]))

【问题讨论】:

  • 可能相关:stackoverflow.com/questions/51810908/…。诚然,我对 Google colab 知之甚少,但这似乎是一个类似的问题。
  • 我使用了相同的方法,但它不起作用
  • 至少从您在问题中写的内容来看,您还没有完成重建步骤。
  • 对不起,我这样做了,但忘了在这里提及

标签: python matplotlib fonts google-colaboratory


【解决方案1】:
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']

我认为这会奏效。

【讨论】:

    猜你喜欢
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    相关资源
    最近更新 更多