matplotlib 中文字体 设置

浏览: 2578

方法一、修改matplotlibrc配置字体

1. 定位matplotlib的字体库路径

locate -b '\mpl-data'

或者在 python环境下

import matplotlib
matplotlib.matplotlib_fname()

/cnn/.pyenv/versions/3.6.5/envs/env3web/lib/python3.6/site-packages/matplotlib/mpl-data

2. 将微软雅黑字体文件msyh.ttf,拷贝到该目录的fonts/ttf下

cp /mnt/share/msyh.ttf /cnn/.pyenv/versions/3.6.5/envs/env3web/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf

3. 删除 matplotlib 缓存

cd ~/.cache/matplotlib
rm * -r

4. 修改matplotlibrc文件:修改font.sans-serif 加微软雅黑字体

font.sans-serif     : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

5. 重启程序或在 jupyter中restart the kernel

方法二、用plt.rcParams设置

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
plt.xlabel('测试')
plt.show()

方法三、用FontProperties,并指定字体文件

from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
font = FontProperties(fname='/mnt/share/code/font/MSYH.TTC', size=20)
plt.title('测试', fontproperties=font)

字体安装

sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv
会将 /usr/share/fonts 下的字体安装一遍

查看系统中文字体

fc-list :lang=zh

pandas_profiling 中文字体设置

# 没起作用
pip show --verbos pandas_profiling # 找到 pandas_profiling 所在位置
vi visualisation/pandas_profiling.mplstyle 
# 也加这段话起作用
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
推荐 0
本文由 safa 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册