matplotlib默认字体设置

使用matplotlib的pyplot时,是支持unicode的,但默认字体是英文字体,致使中文没法正常显示。
解决方法有两种。windows

一种是手动指定,在代码中字体

from matplotlib.font_manager import FontProperties  
font_song = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)

使用文字时指定参数 fontproperties=font_song 便可。code

另外一种是修改配置文件中的默认字体,须要修改 matplotlibrc 文件。
这一文件的位置能够使用 matplotlib.matplotlib_fname() 查看。
若是没有修改会在site-package中。
在默认的matplotlibrc中有注释,建议保存在$HOME/.matplotlib/matplotlibrc下(win环境),防止在更新包时配置文件被覆盖。
经过 font.family : sans-serif 能够指定默认字体族为 sans-serif
以后在 font.sans-serif 的配置列表中最前加入中文字体便可。
但使用的中文字体应该为中文字体的名称。
查看系统的名称能够经过font_manager查看。
[ f.name for f in matplotlib.font_manager.fontManager.ttflist ]
[ f.name for f in matplotlib.font_manager.fontManager.afmlist ]
这两个列表。
我使用的win10没有SimSun(宋体),只能使用STSong,不知是何缘由。
另外微软官网字体里也找不到win10信息(官网连接)。unicode