# 完整的程式加上註解 ``` # -*- coding: UTF-8 -*- import pandas as pd import matplotlib.pyplot as plt import os import time import numpy as np dict_year = {} temp04 = [] temp01 = [] temp02 = None myfile = './學習紀錄.csv' data = pd.read_csv(myfile, encoding='utf-8') col_names = data.columns.values.tolist() # print(col_names) # print(len(col_names)) lists = data.values.tolist() rows_num = len(lists) # print(rows_num) # 去除重複的年度 for i in range(rows_num): temp01.append(lists[i][0]) temp02 = set(temp01) temp01 = list(temp02) # print(temp01) # print(lists) # 依據年度建立字典 for year in temp01: temp03 = [] for i in range(rows_num): myyears = lists[i][0] if myyears == year: temp03.append(lists[i][0:]) dict_year[year] = temp03 print(dict_year) # col_lens = len(col_names) # print(col_lens) # 去掉中文字 for string01 in col_names[17:]: print(string01.split('.')[1][2:-3]) temp04.append(string01.split('.')[1][2:-3]) for school_year in temp01: # 依據建立年度建立資料夾 path_name = str(school_year) print(path_name) if not os.path.exists(path_name): os.makedirs(path_name) years_len = len(dict_year[school_year]) for allyear in range(years_len): # print(col_names[6]) # print(dict_year[108][allyear][6]) # print(col_names[17:-1]) # print(dict_year[108][allyear][17:-1]) # print(len(col_names[17:-1])) # print(len(dict_year[108][allyear][17:-1])) # 在windows 10 底下先將字型安裝在Python安裝路徑\Lib\site-packages\matplotlib\mpl-data\fonts\ttf底下; # 刪除C:\Users\本機帳戶\.matplotlib\fontlist-v330.json # 設定中文字型以免亂碼□□ plt.rcParams['font.sans-serif'] = ['Taipei Sans TC Beta'] # 把存檔的figure size 放大兩倍 plt.rcParams["figure.figsize"] = (16, 12) plt.plot(temp04, dict_year[school_year][allyear][17:], linestyle='--', marker='o', color='b') name = dict_year[school_year][allyear][6]+'-' + \ str(dict_year[school_year][allyear][0]) + \ '('+str(dict_year[school_year][allyear][1])+') 科目' # 打開grid plt.grid(True) # 將y軸定義 y_axis = np.arange(0, 100, 1.0) plt.title(name) plt.yticks(y_axis) plt.ylabel('比例(%)') plt.xticks(temp04, rotation=90) plt.autoscale(True) plt.xlabel('本科目分數級距分布') plt.savefig(os.path.join(path_name, name+'.png'), dpi=300) plt.close() # time.sleep(5) # plt.show() ```