matplotlib显示宋体和Times New Roman

因为绘图需要标准的宋体和Times New Roman,所以图像中的label,title等都需要变化.
参考文档:https://blog.csdn.net/onepiece_dn/article/details/46239581
查看系统已有中文字体,运行命令:
fc-list :lang=zh ,也可以打开ubuntu下的word查看字体
matplotlib显示宋体和Times New Roman_第1张图片根据上图可以得知字体所在的目录位置,所以使用uming字体的Python代码可以写成:

# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt 
import matplotlib as mpl 
from matplotlib.font_manager import FontProperties
zhfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/arphic/uming.ttc
') 
plt.plot([1, 2, 3]) 
plt.xlabel(u'x轴标签', fontproperties=zhfont) 
plt.show()

使用上面代码就可以显示中文了,还需要注意一些细节:

  1. 在中文字符串前面加u
  2. 在python代码的最上方加上编码声明# -- coding:utf-8 --,否则在shell运行会出错
  3. 在python中,字体单位貌似是px,与pt间的换算关系为1pt=4/3px

所以由上面代码可见,为了正确显示字体,需要预先安装字体,比如SimSun(宋体)和Times New Roman,所以接下来就是怎么安装字体的问题.

  • 1.安装Times New Roman

参考文档:https://blog.csdn.net/u014712482/article/details/80568540
Times New Roman在MSfont库中,Ubuntu没有默认安装,所以需要安装

1. sudo apt-get update
2. sudo apt-get install ttf-mscorefonts-installer #命令行安装ttf-mscorefonts-installer,
当提示用户许可协议时按Tab键选OK,未报错则安装成功,去目录/usr/share/fonts/truetype下看到已经安装成功。
3. sudo fc-cache -f -v #刷新字体缓存

经过以上步骤LibreOffice可以看到Times New Roman字体了,还需要清空Python matplot的字体缓存,即cache/matplotlib目录里的文件,不好找的话打开一个Python IED输入命令

import matplotlib as plt
plt.get_cachedir()

直接删除得到的目录,等你下次运行matplot时,它就又自动生成啦,跟Python包原理一样。

$ sudo rm -rf /home/usrname/.cache/matplotlib

接下来再次运行我们的Python matplot的画图程序发现已经可以指定Times New Roman字体了.但是如果还需要其他字体,岂不是每种都要一个一个安装?下面介绍宋体的安装,值得注意的是,该方法具有通用性,Times New Roman也可以用下面方法安装!

  • 2.安装宋体(SimSun)(推荐)(所有字体均可使用此方法,包括Times New Roman)

参考文档:https://blog.csdn.net/qq183837971/article/details/78235144
方法的核心思想就是从windows下面搬字体,这就意味着windows有什么字体,Ubuntu下也可以有!

  1. windows下面安装有各种需要的字体(在C:/Windows/Fonts目录下),则从windows下面拷出想要的字体到Ubuntu的~/font/
  2. 然后在ubuntu下创建文件夹,用来安置这些字体
sudo mkdir -p /usr/share/fonts/winFonts
  1. 拷贝字体到该文件夹下
sudo cp ~/font/* /usr/share/fonts/winFonts/
  1. 改变权限
sudo chmod 666 /usr/share/fonts/winFonts/*
  1. 安装
cd /usr/share/fonts/winFonts/
sudo mkfontscale (创建字体的fonts.scale文件,它用来控制字体旋转缩放)
sudo mkfontdir (创建字体的fonts.dir文件,它用来控制字体粗斜体产生)
sudo fc-cache -fv (建立字体缓存信息,也就是让系统认识)

此时再次运行fc-list :lang=zh即可看到,说明安装成功

/usr/share/fonts/winFonts/simsun.ttc: 宋体,SimSun:style=常规,Regular

则Python程序如下(不会报错):

song = FontProperties(fname='/usr/share/fonts/winFonts/simsun.ttc')  # 宋体
fontcn = {'fontproperties': song, 'size': 14, 'weight': 'normal'}  # 1pt = 4/3px
plt.title(u'气体浓度', fontdict=fontcn)

但是这样做只可以每次都单独设置,设置全局字体也很重要,比如刻度的字体!
所以可以使用下面的方法:

import matplotlib as mpl
# 设定全局字体
mpl.rcParams.update({
    'font.family': 'sans-serif',
    'font.sans-serif': ['Times New Roman'],
    })

上述程序不会报错,但是很奇怪,改成SimSun就会报错,'family’依然不能识别SimSun,猜测原因是SimSun是ttc后缀的,像Times New Roman和DejaVuSans都是ttf后缀,但是好在全局字体可以设定了,Times New Roman可以满足基本要求了!
最后程序变成:

mpl.rcParams.update({
    'font.family': 'sans-serif',
    'font.sans-serif': ['Times New Roman'],
    })  # 设置全局字体
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

song = FontProperties(fname='/usr/share/fonts/winFonts/simsun.ttc')  # 宋体
roman = FontProperties(fname='/usr/share/fonts/winFonts/times.ttf')  # Times new roman
fontcn = {'fontproperties': song, 'size': 14}  # # 此处使用'family': 'SimSun'仍会报错
# fonten = {fontproperties': roman,, 'size': 14}  # 或者下面做法也可以
fonten = {'family': 'Times New Roman', 'size': 14}  # 1pt = 4/3px

plt.title(u'浓度', fontdict=fontcn)
plt.xlabel(u'x12345 (m)', fontdict=fonten)
plt.ylabel(u'y (m)', fontdict=fonten)

设置刻度的做法还可以是:
参考文档:https://blog.csdn.net/u010358304/article/details/78906768

ax = plt.gca()
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]

但是由于默认字体已经选择为 Times New Roman所以已经不需要了!
最后,其他参考文档(不错,但是好像没帮上忙):
http://supercalifragilisticexpiadocious.com/archives/277

你可能感兴趣的:(Python,MatPlotlib,Python,MatPlotlib,字体设置)