1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 # 加載數據 5 res = np.load("./國民經濟核算季度數據.npz", allow_pickle=True) 6 # for tmp in res: 7 # print(tmp) 8 columns = res["columns"] 9 values = res["values"] 10 11 print("columns:\n", columns) 12 print("values:\n", values) 13 14 # 繪圖三部曲 15 # 1、創建畫布 16 plt.figure() 17 # 增加RC參數 18 # 默認不支持中文 19 # 修改RC參數,來讓其支持中文 20 plt.rcParams['font.sans-serif'] = 'SimHei' 21 plt.rcParams['axes.unicode_minus'] = False 22 23 # 2、繪圖 24 # 橫軸---時間(直接繪制的時候,不允許使用中文)---先用序號來代替時間 25 # 縱軸----生產總值 26 # x = values[:, 0] 27 # print("x:\n", x) 28 # 自己生成 29 x = np.arange(1, values.shape[0] + 1) 30 print("x:\n", x) 31 32 # y1 = values[:, 3] 33 # y2 = values[:, 4] 34 # y3 = values[:, 5] 35 # 36 # print("y1:\n", y1) 37 # print("y2:\n", y2) 38 # print("y3:\n", y3) 39 40 y = values[:, 3:6] 41 42 # 繪圖---自己可以構建各種rc來區別點線 43 # plt.plot(x, y1) 44 # plt.plot(x, y2) 45 # plt.plot(x, y3) 46 47 # 注意: 在折線圖中,一個橫坐標,可以匹配多個縱坐標 48 plt.plot(x,y) 49 50 # 增加標題 51 plt.title("2000-2017年各產業季度生產總值走勢圖") 52 53 legend = [tmp[:4] for tmp in columns[3:6]] 54 # 增加圖例 55 plt.legend(legend, loc=2) 56 57 # 增加橫軸名稱 58 plt.xlabel("季度") 59 60 # 增加縱軸名稱 61 plt.ylabel("生產總值(億元)") 62 63 # 設置橫軸刻度 64 # 65 xticks = values[:, 1] 66 # plt.xticks(x, xticks, rotation=75) 67 plt.xticks(x[::4], xticks[::4], rotation=45, horizontalalignment="center") 68 69 # 3、圖形展示 70 plt.show() 71 72 plt.savefig("./2000-2017年各產業季度生產總值走勢圖.png") 73 74 # 趨勢--->一般用於 某個產品的銷量、某件東西上線人數隨時間變化趨勢