天天看点

解决matplotlib 堆叠图中文乱码

安装缺失的字体,最后记住 _rebuild() #reload一下

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.font_manager import _rebuild

_rebuild() #reload一下


matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
# matplotlib.font_manager.FontProperties(fname=r"/home/romulus/Downloads")
# matplotlib.font_manager.FontProperties(directory=r"/home/romulus/Downloads/SimHei.ttf")
#
# import matplotlib
# matplotlib.rcParams['font.family'] = 'sans-serif'
# matplotlib.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'# 中文设置成宋体,除此之外的字体设置成New Roman
#



label_list = ['2014', '2015', '2016', '2017']
num_list1 = [20, 30, 15, 35]
num_list2 = [15, 30, 40, 20]
x = range(len(num_list1))
rects1 = plt.bar(left=x, height=num_list1, width=0.45, alpha=0.8, color='red', label="一部门")
rects2 = plt.bar(left=x, height=num_list2, width=0.45, color='green', label="二部门", bottom=num_list1)
plt.ylim(0, 80)
plt.ylabel("num")
# plt.ylabel("数量")
plt.xticks(x, label_list)
plt.xlabel("year")
# plt.xlabel("年份")
plt.title("XXcompany")
# plt.title("某某公司")

plt.legend()
plt.show()