1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
import pandas as pd import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties %matplotlib inline
# 找到自己电脑的字体路径,然后修改字体路径 font = FontProperties(fname='/Library/Fonts/Heiti.ttc')
header_list = ['方程组', '函数', '导数', '微积分', '线性代数', '概率论', '统计学'] py3_df = pd.read_excel('py3.xlsx', header=None, skiprows=[0, 1], names=header_list) # 处理带有NaN的行 py3_df = py3_df.dropna(axis=0) print(py3_df)
# 自定义映射 map_dict = { '不会': 0, '了解': 1, '熟悉': 2, '使用过': 3, }
for header in header_list: py3_df[header] = py3_df[header].map(map_dict)
unable_series = (py3_df == 0).sum(axis=0) know_series = (py3_df == 1).sum(axis=0) familiar_series = (py3_df == 2).sum(axis=0) use_series = (py3_df == 3).sum(axis=0)
unable_label = '不会' know_label = '了解' familiar_label = '熟悉' use_label = '使用过' for i in range(len(header_list)): bottom = 0
# 描绘不会的条形图 plt.bar(x=header_list[i], height=unable_series[i], width=0.60, color='r', label=unable_label) if unable_series[i] != 0: plt.text(header_list[i], bottom, s=unable_series[i], ha='center', va='bottom', fontsize=15, color='white') bottom += unable_series[i]
# 描绘了解的条形图 plt.bar(x=header_list[i], height=know_series[i], width=0.60, color='y', bottom=bottom, label=know_label) if know_series[i] != 0: plt.text(header_list[i], bottom, s=know_series[i], ha='center', va='bottom', fontsize=15, color='white') bottom += know_series[i]
# 描绘熟悉的条形图 plt.bar(x=header_list[i], height=familiar_series[i], width=0.60, color='g', bottom=bottom, label=familiar_label) if familiar_series[i] != 0: plt.text(header_list[i], bottom, s=familiar_series[i], ha='center', va='bottom', fontsize=15, color='white') bottom += familiar_series[i]
# 描绘使用过的条形图 plt.bar(x=header_list[i], height=use_series[i], width=0.60, color='b', bottom=bottom, label=use_label) if use_series[i] != 0: plt.text(header_list[i], bottom, s=use_series[i], ha='center', va='bottom', fontsize=15, color='white')
unable_label = know_label = familiar_label = use_label = ''
plt.xticks(header_list, fontproperties=font) plt.ylabel('人数', fontproperties=font) plt.title('Python3期数学摸底可视化', fontproperties=font) plt.legend(prop=font, loc='upper left') plt.show()
|