简体   繁体   English

Matplotlib 基本找不到 fonts

[英]Matplotlib cannot find basic fonts

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在 miniconda 虚拟环境中的 Python 3 上使用 matplotlib 版本 2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在研究一个没有 root 权限的 unix 科学计算集群。 I am generally executing python code through an ipython notebook.我通常通过 ipython 笔记本执行 python 代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用 Times New Roman 字体,但即使在删除了我从这里找到的字体缓存文件 (fontList.py3k.cache) 之后:

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

The commands:命令:

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

have no effect, I get the same error as above.没有效果,我得到与上面相同的错误。 The true type fonts directory:真型fonts目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy里面只有 40 个 fonts 类型:DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加额外的 fonts? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I fixed it by setting pyplot parameter: 我通过设置pyplot参数修复它:

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

so you can use fonts (Helvetica, Times New Roman) like the following: 所以你可以使用如下字体(Helvetica,Times New Roman):

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

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

I am using matplotlib version 2.0.0 on Python 3 in a miniconda virtual environment.我在miniconda虚拟环境中的Python 3上使用了matplotlib版本2.0.0。 I am working on a unix scientific computing cluster where I don't have root privileges.我正在一个没有root特权的unix科学计算集群上工作。 I am generally executing python code through an ipython notebook.我通常通过ipython笔记本执行python代码。 If I do a basic command such as:如果我执行基本命令,例如:

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

I get an error message:我收到一条错误消息:

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

I would like to be able to use a Times New Roman font but even after deleting the Font cache file (fontList.py3k.cache) which I find from here:我希望能够使用Times New Roman字体,但即使删除了从此处找到的字体缓存文件(fontList.py3k.cache),也可以使用它:

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

The commands:命令:

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

have no effect, I get the same error as above.没有任何效果,我得到了与上述相同的错误。 The true type fonts directory:真正的字体目录:

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

only has 40 fonts in it of the type: DejaVuSerif,DejaVuSans,STIX,cmb, cmt, cmy其中只有40种字体类型:DejaVuSerif,DejaVuSans,STIX,cmb,cmt,cmy

Any idea what could be going on and how I can add additional fonts?知道会发生什么以及如何添加其他字体吗? Thanks!谢谢!

None of the above worked for me.以上都不适合我。 I had to combine the provided solutions.我必须结合提供的解决方案。

I run Jupyter from PyCharm in Windows 10 .我从Windows 10中的 PyCharm 运行Jupyter Here is my workaround.这是我的解决方法。 Before doing these steps, import the font manager via import matplotlib.font_manager :在执行这些步骤之前,请通过import matplotlib.font_manager导入字体管理器:

  1. Find font directory => matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')查找字体目录 => matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
  2. Put your font file (ttf. file) to that directory.将您的字体文件(ttf. 文件)放入该目录。
  3. Find font config file: matplotlib.matplotlib_fname()查找字体配置文件: matplotlib.matplotlib_fname()
  4. Edit to set font type: pdf.fonttype: 42 # Output Type 3 (Type3) or Type 42 (TrueType)编辑以设置字体类型: pdf.fonttype: 42 # Output Type 3 (Type3) or Type 42 (TrueType)
  5. Run PyCharm .运行PyCharm
  6. After you start kernel, cache files will be generated.启动 kernel 后,会生成缓存文件。 Clean \matplotlib\__pycache__ folder.清理\matplotlib\__pycache__文件夹。
  7. Set the font you want to use via:通过以下方式设置要使用的字体:
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Lin Libertine']
  1. Run the code to generate plots.运行代码以生成绘图。

If you restart your kernel, if will generate cache files, hence you need to remove them again.如果您重新启动 kernel,如果会生成缓存文件,因此您需要再次删除它们。

My situation is that I have JupyterHub running in Docker and I need to use matplotlib to plot and display some Chinese characters as titles.我的情况是我在 Docker 中运行 JupyterHub,我需要使用matplotlib到 plot 并显示一些汉字作为标题。

Here's how I install new fonts and get new fonts worked( No need to restart the Jupyter container ):这是我安装新 fonts 并获得新 fonts 工作的方法(无需重新启动 Jupyter 容器):

Docker Image version: Docker 镜像版本:

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

All operations are executed inside the container.所有操作都在容器内执行。

  1. download the fonts to /.../python3.7/site-packages/matplotlib/mpl-data/fonts/ttf将 fonts 下载到/.../python3.7/site-packages/matplotlib/mpl-data/fonts/ttf

  2. rebuild the font cache in a notebook.在笔记本中重建字体缓存。 ~/.cache/matplotlib/ ( AttributeError: module 'matplotlib' has no attribute 'font_manager' ). ~/.cache/matplotlib/ ( AttributeError: module 'matplotlib' has no attribute 'font_manager' )。

import matplotlib.font_manager
matplotlib.font_manager._rebuild()

the font cache is per-user base( /home/{username}/.cache/matplotlib/ ), so other people might need to rebuild the cache too.字体缓存是基于每个用户的( /home/{username}/.cache/matplotlib/ ),因此其他人可能也需要重建缓存。

  1. use the new font.使用新字体。 eg例如
import matplotlib.pyplot as plt

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

You might need to restart the notebook/kernel in step 2 and 3.您可能需要在第 2 步和第 3 步中重新启动笔记本/内核。

Notes:笔记:

  • I tried sudo apt install msttcorefonts -qq but I don't think that works.我试过sudo apt install msttcorefonts -qq但我认为这不起作用。 Neither do I know what has been installed by that package.我也不知道那个 package 安装了什么。 apt-file list msttcorefonts returns nothing. apt-file list msttcorefonts什么也不返回。
    Why apt-file list msttcorefonts returns nothing? 为什么apt-file list msttcorefonts什么也不返回?

  • There is a configuration file /.../python3.7/site-packages/matplotlib/mpl-data/matplotlibrc on which one can add font.sans-serif fonts.有一个配置文件/.../python3.7/site-packages/matplotlib/mpl-data/matplotlibrc可以在其中添加font.sans-serif fonts。 But without modifying it, the new fonts also work.但无需修改,新的 fonts 也可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM