matplotlib图例中文乱码问题
下载中文字体
下载SimHei.ttf字体
添加字体到matplotlib字体文件夹
以Anaconda为例
base环境目录为
/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts
其他环境目录为
/anaconda3/envs/env-name/lib/python3.7/site-packages/matplotlib/mpl-data/fonts
# 输出字体路径
matplotlib.matplotlib_fname()
修改配置文件matplotlibrc
位置为matplotlib/mpl-data
取消注释
font.family : sans-serif
取消注释,并添加SimHei
font.sans-serif : SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
如果负号’-'显示为方块,则取消注释下面这行并修改为False
axes.unicode_minus : False
重新加载字体
执行
from matplotlib.font_manager import _rebuild
_rebuild()
也可以删除.matplotlib目录下的fontList.json和tex.cache
# 输出缓存路径
matplotlib.get_cachedir()
针对matplotlib在显示中文图例时出现乱码的问题,解决方案包括:下载SimHei.ttf中文字体,将其添加到matplotlib字体文件夹(如Anaconda环境的相应路径),修改matplotlibrc配置文件取消中文字体注释,并根据需要调整负号显示设置,最后重新加载字体或清理缓存。
1186

被折叠的 条评论
为什么被折叠?



