matplotlib 中文问题

转自: matplotlib图例中文乱码? - pythonic生物人的回答 - 知乎 https://www.zhihu.com/question/25404709/answer/2455652871

  • 最近发现一个很nice的Matplotlib字体管理工具-mplfonts,可彻底解决Matplotlib亚洲字体 (中文、韩文及日文等)乱码问题、轻松管理Matplotlib字体库;
    本文详细介绍mplfonts使用。

mplfonts安装

pip install mplfonts -i https://pypi.tuna.tsinghua.edu.cn/simple

mplfonts设置

以下两种方法等效,

方法1、在终端设置:

mplfonts init 即可。

方法2、在代码中加入

from mplfonts.bin.cli import init
init()

mplfonts解决matplotlib中文显示问题, 使用方式

from mplfonts import use_font
use_font('Noto Serif CJK SC')#指定中文字体
# 主要是上面两行

plt.style.use('fivethirtyeight')
plt.figure(dpi=120)
plt.plot([1, 2, 3])
plt.title('中文', color='red')
plt.show()

mplfonts管理matplotlib字体库

参考原文

附SeiHei 字体下载

SimHei字体下载

posted @ 2022-11-15 12:12  张璨  阅读(92)  评论(0编辑  收藏  举报