Python 更改 matplotlib 的默认字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21461155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:54:23  来源:igfitidea点击:

change matplotlib's default font

pythonfontsmatplotlibdefaults

提问by gozzilli

I'm trying to change matplotlib's default font to Helvetica Neue. On my Mac with EPD/Canopy everything worked fine some time ago.

我正在尝试将 matplotlib 的默认字体更改为 Helvetica Neue。在我的带有 EPD/Canopy 的 Mac 上,前段时间一切正常。

Trying to do the same on ubuntu now and it's not working.

现在尝试在 ubuntu 上做同样的事情,但它不起作用。

This is what I did:

这就是我所做的:

  1. Installed Helvetica Neue

    $ fc-match 'Helvetica Neue':Light
    HelveticaNeue-Light.otf: "Helvetica Neue" "細體"
    
  2. Converted the odt/dfont into ttf:

    fondu -show HelveticaNeue.dfont
    
  3. changed matplotlibrc to

    $ cat ~/.config/matplotlib/matplotlibrc
    ...
    font.family: Helvetica Neue
    

    I also tried with:

    font.family: sans-serif
    font.sans-serif: Helvetica Neue
    
  4. I removed the font cache

    rm ~/.config/matplotlib/fontList.cache
    
  1. 安装 Helvetica Neue

    $ fc-match 'Helvetica Neue':Light
    HelveticaNeue-Light.otf: "Helvetica Neue" "細體"
    
  2. 将 odt/dfont 转换为 ttf:

    fondu -show HelveticaNeue.dfont
    
  3. 将 matplotlibrc 更改为

    $ cat ~/.config/matplotlib/matplotlibrc
    ...
    font.family: Helvetica Neue
    

    我也试过:

    font.family: sans-serif
    font.sans-serif: Helvetica Neue
    
  4. 我删除了字体缓存

    rm ~/.config/matplotlib/fontList.cache
    

But none of these steps are working for me.

但是这些步骤都不适合我。

    $ python -c 'from  matplotlib import pyplot as plt; plt.plot(1); plt.savefig("/tmp/test.png")'
    /usr/local/lib/python2.7/dist-packages/matplotlib-1.3.0-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:1236: 
    UserWarning: findfont: Font family ['Helvetica Neue'] not found. Falling back to Bitstream Vera Sans

(prop.get_family(), self.defaultFamily[fontext]))

(prop.get_family(), self.defaultFamily[fonttext]))

Version is 1.3.0

版本是 1.3.0

    $ python -c 'import matplotlib; print  matplotlib.__version__'
    1.3.0

I also tried moving the fonts to ~/.config/matplotlib/fonts/ttfbut it didn't work.

我也尝试将字体移动到,~/.config/matplotlib/fonts/ttf但没有用。



EDIT:As suggested I tried selecting a specific font for a specific text.

编辑:按照建议,我尝试为特定文本选择特定字体。

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

path = '/home/<myusername>/.fonts/HelveticaNeue.ttf'

prop = font_manager.FontProperties(fname=path)
prop.set_weight = 'light'
mpl.rcParams['font.family'] = prop.get_name()
mpl.rcParams['font.weight'] = 'light'

fig, ax = plt.subplots()
ax.set_title('Text in a cool font', fontproperties=prop, size=40)
plt.savefig('/tmp/test2.png')

But it makes no difference.

但这没什么区别。

/usr/local/lib/python2.7/dist-packages/matplotlib-1.3.0-py2.7-linux-x86_64.egg/matplotlib/font_manager.py:1236: 
UserWarning: findfont: Font family ['Helvetica Neue'] not found. Falling back to Bitstream Vera Sans

However I seem to experience this problem only with this Helvetica/Helvetica Neue font. (prop.get_family(), self.defaultFamily[fontext]))

然而,我似乎只有使用这种 Helvetica/Helvetica Neue 字体才会遇到这个问题。(prop.get_family(), self.defaultFamily[fonttext]))

回答by Brian

This won't change you font permanently, but it's worth a try

这不会永久改变你的字体,但值得一试

matplotlib.rc('font', family='sans-serif') 
matplotlib.rc('font', serif='Helvetica Neue') 
matplotlib.rc('text', usetex='false') 
matplotlib.rcParams.update({'font.size': 22})

回答by KyungHoon Kim

Ubuntu 14.04 LTS

Ubuntu 14.04 LTS

Upload the fonts

上传字体

sudo cp NotoSansKR-Regular.otf /usr/share/fonts/

Update the cache of font

更新字体缓存

sudo fc-cache -fv

You can check the font list

您可以查看字体列表

fc-list

Restart ipython, etc. Check the font list

重启ipython等查看字体列表

[f.name for f in matplotlib.font_manager.fontManager.ttflist]

Take a your font name

取一个你的字体名称

import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['font.family'] = 'Noto Sans Korean'

Draw

plt.title(u'?? ??')
plt.xlabel(u'?? ? ??')
plt.plot(range(5))

回答by jmp

The font cache shows up in a different place for me (.cache/matplotlib/fontList.cache). And before I actually had three of them in different places somehow :/

字体缓存对我来说显示在不同的地方(.cache/matplotlib/fontList.cache)。在我实际上以某种方式将其中三个放在不同的地方之前:/

maybe try searching for it in your home directory:

也许尝试在您的主目录中搜索它:

find ~/ -name fontList.cache -exec rm {} \;

回答by Lyle

Kim already introduced dynamic solution works perfectly, and here's two other ways doing the same in static.

Kim 已经介绍了动态解决方案工作得很好,这里有另外两种方法在静态中做同样的事情。

First, you may put a line to rc file for matplotlib . Refer to this pagefor more information about locating the file and detailed settings.

首先,您可以在 rc 文件中为 matplotlib 放置一行。有关查找文件和详细设置的更多信息,请参阅此页面

font.family : NanumGothic

Second, if you are working with ipython, you can put some commands for font setting to a configuration file for the interactive shell. Find the file named ipython_config.py which usually located under ~/.ipython/somewhere. Then add two more lines to the list, c.InteractiveShellApp.exec_lines .

其次,如果您正在使用 ipython,您可以将一些用于字体设置的命令放入交互式 shell 的配置文件中。找到名为 ipython_config.py 的文件,它通常位于 ~/.ipython/somewhere 下。然后再向列表中添加两行 c.InteractiveShellApp.exec_lines 。

c.InteractiveShellApp.exec_lines = [
    "import matplotlib as mpl",
    "mpl.rcParams['font.family'] = 'NanumGothic'"
]

Former always works whatever environment you run your shell script on as it loads the font when your script imports matplotlib .

前者始终适用于您运行 shell 脚本的任何环境,因为它会在您的脚本导入 matplotlib 时加载字体。