change matplotlib's default font

☆樱花仙子☆ 提交于 2019-11-28 21:12:45

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

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

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

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 :/

maybe try searching for it in your home directory:

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

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

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

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 .

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 .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!