1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 def draw_data(n, a, b, d, values, fig, plt, t, l, cl): 5 fig.add_subplot(2, 2, n) 6 x = values[d, a:b+1] 7 # explode = (0.01) 8 plt.pie(x, labels=l, colors=cl, autopct="%.2f%%", shadow=False, labeldistance=1.05) 9 plt.axis("equal") 10 plt.legend(l) 11 plt.title(t) 12 plt.savefig("./2000和2017年各产业生产总值占比饼图.png") 13 return plt 14 15 def show_data(): 16 fig = plt.figure(figsize=(20, 12), dpi=120) 17 plt.rcParams[‘font.sans-serif‘] = ‘SimHei‘ 18 plt.rcParams[‘axes.unicode_minus‘] = False 19 # plt.subplots_adjust(hspace=0.3) 20 return fig, plt 21 22 def save_data(): 23 plt.savefig("./2000-2017年各个产业、行业的柱状图.png") 24 plt.show() 25 return None 26 27 def build_data(): 28 res = np.load("./国民经济核算季度数据.npz", allow_pickle=True) 29 columns = res["columns"] 30 values = res["values"] 31 # print(columns) 32 # print(values) 33 return columns, values 34 35 def main(): 36 columns, values = build_data() 37 fig, plt = show_data() 38 title1 = "2000年第一季度国民总值产业构成分布饼图" 39 title2 = "2017年第一季度国民总值产业构成分布饼图" 40 labels1 = [tmp[:4] for tmp in columns[3:6]] 41 labels2 = [tmp[:2] for tmp in columns[6:]] 42 colors1 = ["#53868B", "#7AC5CD", "#8EE5EE"] 43 colors2 = ["#CD9B9B", "#EEB4B4", "#8B658B"] 44 colors3 = ["#548B54", "#7CCD7C", "#90EE90", "#9AFF9A", "#2E8B57", "#43CD80", "#4EEE94", "#54FF9F", "#698B69"] 45 colors4 = ["#CD950C", "#EEAD0E", "#FFB90F", "#8B6914", "#CD9B1D", "#EEB422", "#FFC125", "#8B7500", "#CDAD00"] 46 draw_data(1, 3, 5, 0, values, fig, plt, title1, labels1, colors1) 47 draw_data(2, 3, 5, 68, values, fig, plt, title2, labels1, colors2) 48 draw_data(3, 6, 14, 0, values, fig, plt, title1, labels2, colors3) 49 draw_data(4, 6, 14, 68, values, fig, plt, title2, labels2, colors4) 50 save_data() 51 52 53 if __name__ == ‘__main__‘: 54 main()
原文地址:https://www.cnblogs.com/Tree0108/p/12115967.html
时间: 2024-10-10 22:37:50