linux安装中文字体支持matplot显示

这里以安装simihei字体为例

  1. 安装 fontconfig

sudo yum -y install fontconfig  # fontconfig字体重写和配置使用程序
  1. 下载字体

cd /usr/share/fonts # 切换到字体路径
wget https://www.wfonts.com/download/data/2014/06/01/simhei/simhei.zip # 下载字体
unzip simhei.zip -d simhei # 解压到指定路径
  1. 刷新字体缓存

fc-cache -fv
  1. 查看已安装字体

fc-list |grep simhei
# /usr/share/fonts/simhei/SimHei.ttf: SimHei,黑体:style=Regular
# /usr/share/fonts/simhei/chinese.simhei.ttf: SimHei,黑体:style=Regular
  1. 复制到python路径

import matplotlib # run in python
print(matplotlib.matplotlib_fname()) # run in python
# /root/anaconda3/lib/python3.9/site-packages/matplotlib/mpl-data/matplotlibrc
cp /usr/share/fonts/simhei/SimHei.ttf /root/anaconda3/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf/ # 这里根据上一步的结果自行调整

rm ~/.cache/matplotlib/fontlist-v330.json # 清除缓存
  1. 中文画图示例

import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.plot(x, y)

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
plt.title("中文标题") # 中文标题,如果显示不成功,重启Python运行下。
plt.show()

猜你喜欢

转载自blog.csdn.net/lpfangle/article/details/128967439
今日推荐