Linux installs Chinese fonts to support matplot display

Here is an example of installing the simihei font

  1. install fontconfig

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

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

fc-cache -fv
  1. View installed fonts

fc-list |grep simhei
# /usr/share/fonts/simhei/SimHei.ttf: SimHei,黑体:style=Regular
# /usr/share/fonts/simhei/chinese.simhei.ttf: SimHei,黑体:style=Regular
  1. Copy to python path

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. Example of Chinese drawing

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()

Guess you like

Origin blog.csdn.net/lpfangle/article/details/128967439