先介绍一下我的开发环境:
系统平台: mac OS 10.15
anaconda 3:
matplotlib 2.0.2
修改matplotlib绘制的图表的默认配置有三种方案( 以加入中文支持为例 ):
1. 方案一: 通过 font_manager来指定本机安装的中文字库,请注意确保本机对应的路径下已安装此字库( fm.FontProperties(fname='/Library/Fonts/Arial Unicode.ttf') .
#中文文本的解决方案:
#matplot显示图例中的中文问题 : https://www.zhihu.com/question/25404709/answer/67672003
import matplotlib.font_manager as fm
#mac中的字体问题请看: https://zhidao.baidu.com/question/161361596.html
# 或 /System/Library/Fonts
myfont = fm.FontProperties(fname='/Library/Fonts/Arial Unicode.ttf') # C:\Windows\Fonts\simsun
plt.axis([0,5,0,20])
plt.title('我的第一个图表',fontsize=20,fontname='Times New Roman',fontproperties=myfont) # 要便用 fontproperties来指定中文字体
plt.xlabel('计数',fontproperties=myfont)
plt.ylabel('平方值',fontproperties=myfont)
plt.text(1,1.5,'一',fontproperties=myfont)
plt.text(2,4.5,'二',fontproperties=myfont)
plt.text(3,9.5,'三',fontproperties=myfont)
plt.text(4,16.5,'四')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()
2. 方案二: 通过指定 plt.style.use(<style-name>) 或 plt.rcParams 配置样式,则对这个 plt 所绘制的所有的图有效果.这两种方案配置的文档请参考: https://matplotlib.org/3.3.0/tutorials/introductory/customizing.html?highlight=text.usetex#
具体的配置项请参考matplotlib的安装目录下的文件matplotlibrc,此文件的位置可以使用以下命令查看:
>>> import matplotlib
>>> matplotlib.matplotlib_fname()
'/home/foo/.config/matplotlib/matplotlibrc'
此文件的具体内容也可在matplotlib的官网查看,地址: https://matplotlib.org/3.3.0/tutorials/introductory/customizing.html?highlight=text.usetex#matplotlibrc-sample
以下是我的测试代码:
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.rcParams['axes.grid']=True
#以上其它的配置项可以参考官方文档
#rcParams可以指定绘图中所有的外观配置,具体的参数请参见:
plt.axis([0,5,0,20])
plt.title('我的第一个图表',fontsize=20) # 要便用 fontproperties来指定中文字体
plt.xlabel('计数')
plt.ylabel('平方值')
plt.text(1,1.5,'一')
plt.text(2,4.5,'二')
plt.text(3,9.5,'三')
plt.text(4,16.5,'四')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()
分析 字体家族共有五种分别是:
#font.serif: DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif #font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif #font.cursive: Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive #font.fantasy: Comic Neue, Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy #font.monospace: DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
默认的情况下,没有中文字体,这里我安装了一个 SimHei的中文字体到mac中,它属于 sans-serif 字体家族。所以我配置成了
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 ,
这样就可以显示中文了,但这种配置方案只针对当前的plt有效,另起一个进程就无效了.
3. 方案三:通过修改 matplotlibrc 文件来实现全局配置.
这个文件在我这里的位置: /Applications/anocadote/anaconda/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
找到其中的
#font.sans-serif: DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
修改成:
font.sans-serif : SimHei
请注意: 在这个文件中所以以 # 开头的部分都是系统的默认配置。 大家可以参考一下对matplotlib进行一些基础配置。
验证以上全局配置是否成功:
import matplotlib
print(matplotlib.rc_params())
可以得到以下结果,表示配置成功: