一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Pycharm中matplotlib画图中文乱码问题解决方法

时间:2021-01-11 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Pycharm中matplotlib画图中文乱码问题解决方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

Matplotlib

Matplotlib 是Python中类似 MATLAB 的绘图工具,熟悉 MATLAB 也可以很快的上手 Matplotlib。

用的MacOs系统,不过Windows也大同小异

首先去下载SimHei字体:

https://github.com/StellarCN/scp_zh/blob/master/fonts/SimHei.ttf

然后直接双击安装;

将下载的SimHei.ttf移动到你的matplotlib/mpl-data/fonts/ttf/下,路径全称可在Pycharm里使用下面的代码打印出来,我的是/Library/Python/3.8/site-packages/matplotlib/mpl-data/matplotlibrc/mpl-data/fonts/ttf/

import matplotlib
path = matplotlib.matplotlib_fname()
print(path)

然后去编辑/mpl-data/下的matplotlibrc,修改以下内容(去掉前面的#号,第三行修改为False)

font.family   : sans-serif  
font.sans-serif  : SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif 
axes.unicode_minus:False,#作用就是解决负号'-'显示为方块的问题

然后去Pycharm或者python终端里面执行以下命令

from matplotlib.font_manager import _rebuild
_rebuild()

最后在你要使用matplotlib画图的代码中加入如下两句

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

热门栏目