安裝缺失的字型,最後記住 _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()