我用的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 #用来正常显示负号
-
大功告成