在Docker镜像中,使用matplotlib进行绘图时,不展示中文,解决方案如下
下载字体:文章源自懂站帝-http://www.sfdkj.com/12566.html
# 可以把下述命令添加到Dockerfile中
apt-get -y install wget \
&& wget -O /usr/share/fonts/simsun.ttf https://pfh-file-store.oss-cn-hangzhou.aliyuncs.com/simsun.ttf \
&& fc-cache -vf \
&& fc-list
设置matplotlib中文显示问题:文章源自懂站帝-http://www.sfdkj.com/12566.html 把下载好的字体复制到matplotlib的安装目录中
# 每台机器的安装路径都不同,找到自己的安装路径即可
cd /root/miniconda3/lib/python3.9/site-packages/matplotlib/mpl-data
cp /usr/share/fonts/simsun.ttf ./fonts/
matplotlibrc文件做如下修改:文章源自懂站帝-http://www.sfdkj.com/12566.html
# 去掉前面的#
font.family : sans-serif
# 去掉前面的#,并在冒号后面添加
font.sans-serif : simsun
# 去掉前面的#,并将True改为False
axes.unicode_minus : False
删除matplotlib缓存文章源自懂站帝-http://www.sfdkj.com/12566.html
获取缓存路径文章源自懂站帝-http://www.sfdkj.com/12566.html
# 获取缓存路径
import matplotlib
print(matplotlib.get_cachedir())
删除缓存文章源自懂站帝-http://www.sfdkj.com/12566.html
rm -rf /root/.cache/matplotlib/
脚本测试文章源自懂站帝-http://www.sfdkj.com/12566.html
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 10), dpi=100)
game = ['1-G1', '1-G2', '1-G3', '1-G4', '1-G5', '2-G1', '2-G2', '2-G3', '2-G4', '2-G5', '3-G1', '3-G2', '3-G3',
'3-G4', '3-G5', '总决赛-G1', '总决赛-G2', '总决赛-G3', '总决赛-G4', '总决赛-G5', '总决赛-G6']
scores = [23, 10, 38, 30, 36, 20, 28, 36, 16, 29, 15, 26, 30, 26, 38, 34, 33, 25, 28, 40, 28]
plt.plot(game, scores)
plt.xlabel("比赛", fontsize=13)
plt.ylabel("得分", fontsize=13)
plt.title("比赛得分记录")
plt.show()
文章源自懂站帝-http://www.sfdkj.com/12566.html文章源自懂站帝-http://www.sfdkj.com/12566.html