簡體   English   中英

Matplotlib 基本找不到 fonts

[英]Matplotlib cannot find basic fonts

我在 miniconda 虛擬環境中的 Python 3 上使用 matplotlib 版本 2.0.0。 我正在研究一個沒有 root 權限的 unix 科學計算集群。 我通常通過 ipython 筆記本執行 python 代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用 Times New Roman 字體,但即使在刪除了我從這里找到的字體緩存文件 (fontList.py3k.cache) 之后:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有效果,我得到與上面相同的錯誤。 真型fonts目錄:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-packages/matplotlib/mpl-data/fonts/ttf/

里面只有 40 個 fonts 類型:DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy

知道會發生什么以及如何添加額外的 fonts? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我通過設置pyplot參數修復它:

import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'serif'

所以你可以使用如下字體(Helvetica,Times New Roman):

ax.set_title('your_title_text',fontname="Times New Roman", fontsize=20)

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

我在miniconda虛擬環境中的Python 3上使用了matplotlib版本2.0.0。 我正在一個沒有root特權的unix科學計算集群上工作。 我通常通過ipython筆記本執行python代碼。 如果我執行基本命令,例如:

import matplotlib.pyplot as plt
plt.scatter([1,5], [1,5])

我收到一條錯誤消息:

path_to_miniconda/miniconda3/envs/conda34/lib/python3.4/site-
packages/matplotlib/font_manager.py:1297: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to DejaVu Sans
(prop.get_family(), self.defaultFamily[fontext]))

我希望能夠使用Times New Roman字體,但即使刪除了從此處找到的字體緩存文件(fontList.py3k.cache),也可以使用它:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

命令:

mpl.rcParams['font.family'] = ['serif']
mpl.rcParams['font.serif'] = ['Times New Roman']

沒有任何效果,我得到了與上述相同的錯誤。 真正的字體目錄:

path_to_miniconda / miniconda3 / envs / conda34 / lib / python3.4 / site-packages / matplotlib / mpl-data / fonts / ttf /

其中只有40種字體類型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

知道會發生什么以及如何添加其他字體嗎? 謝謝!

以上都不適合我。 我必須結合提供的解決方案。

我從Windows 10中的 PyCharm 運行Jupyter 這是我的解決方法。 在執行這些步驟之前,請通過import matplotlib.font_manager導入字體管理器:

  1. 查找字體目錄 => matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
  2. 將您的字體文件(ttf. 文件)放入該目錄。
  3. 查找字體配置文件: matplotlib.matplotlib_fname()
  4. 編輯以設置字體類型: pdf.fonttype: 42 # Output Type 3 (Type3) or Type 42 (TrueType)
  5. 運行PyCharm
  6. 啟動 kernel 后,會生成緩存文件。 清理\matplotlib\__pycache__文件夾。
  7. 通過以下方式設置要使用的字體:
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Lin Libertine']
  1. 運行代碼以生成繪圖。

如果您重新啟動 kernel,如果會生成緩存文件,因此您需要再次刪除它們。

我的情況是我在 Docker 中運行 JupyterHub,我需要使用matplotlib到 plot 並顯示一些漢字作為標題。

這是我安裝新 fonts 並獲得新 fonts 工作的方法(無需重新啟動 Jupyter 容器):

Docker 鏡像版本:

  • Ubuntu 18.04.3 LTS
  • Python 3.7.6
  • matplotlib.__version__ :3.1.2

所有操作都在容器內執行。

  1. 將 fonts 下載到/.../python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

  2. 在筆記本中重建字體緩存。 ~/.cache/matplotlib/ ( AttributeError: module 'matplotlib' has no attribute 'font_manager' )。

import matplotlib.font_manager
matplotlib.font_manager._rebuild()

字體緩存是基於每個用戶的( /home/{username}/.cache/matplotlib/ ),因此其他人可能也需要重建緩存。

  1. 使用新字體。 例如
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimSun']

您可能需要在第 2 步和第 3 步中重新啟動筆記本/內核。

筆記:

  • 我試過sudo apt install msttcorefonts -qq但我認為這不起作用。 我也不知道那個 package 安裝了什么。 apt-file list msttcorefonts什么也不返回。
    為什么apt-file list msttcorefonts什么也不返回?

  • 有一個配置文件/.../python3.7/site-packages/matplotlib/mpl-data/matplotlibrc可以在其中添加font.sans-serif fonts。 但無需修改,新的 fonts 也可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM