解决matplotlib中文乱码问题方案众多,我认为如下方案是最简单的一个。

1、从电脑中搜索simhei字体,如下示意图是mac检索结果,或者直接搜索simhei.ttf下载字体

matplotlib中文乱码最简单的解决方案_matplotlib

拷贝到指定路径:/path/to/mex/simhei.ttf 

2、matplotlib 加载字体

import matplotlib.pyplot as plt
 import matplotlib.font_manager as fm# 添加字体
 fm.fontManager.addfont('/path/to/mex/simhei.ttf')
 font_size=12
 plt.rcParams['font.family'] = ['sans-serif']
 plt.rcParams['font.sans-serif'] = ['SimHei'] # 散点图标签可以显示中文def plot_with_chinese():
     import matplotlib.pyplot as plt
     import matplotlib.font_manager as fm    plt.figure(figsize=(13, 9))
     # 使用内置字体名称初始化
     plt.text(0.5, 0.5, 'this is 文本')
     # 验证font_properties参数
     plt.annotate('this is 注解', (0.1, 0.1))
     # 验证fontproperties参数
     plt.title("this is 标题")
     plt.xlabel("this is x轴")
     plt.ylabel("this is y轴")
     plt.legend()  # 显示图例
     plt.grid()
     plt.show()
 plot_with_chinese()

效果如下

matplotlib中文乱码最简单的解决方案_搜索_02