分析文件’課程成績.xlsx’,至少要完成內容:
1)每年不同班級平均成績情況
2)不同年份總體平均成績情況
3)不同性別學生成績情況,並分別用合適的圖表展示出三個內容的分析結果。
導入相應的庫
from functools import reduce import xlrd from flask import Flask, jsonify, render_template, request, url_for from pyecharts import Line,Bar,Pie,Radar
python讀取excel文件數據
excel_path="..\\class.xlsx" #打開文件,獲取excel文件的workbook(工作簿)對象 excel=xlrd.open_workbook(excel_path,encoding_override="utf-8") # 返回所有Sheet對象的list all_sheet=excel.sheets() #循環遍歷每個sheet對象存儲表中所有數據 grade_list=[] # 將文件中數據存進grade_list for sheet in all_sheet: for each_row in range(sheet.nrows):#循環打印每一行 # each_row="".join(str(each_row).split()) grade_list.append(sheet.row_values(each_row))
python圖表中文亂碼
#設置中文亂碼 from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei']
列表數據相加求平均
def sum_list(items):
sum_numbers = 0 count = 0 for x in items: sum_numbers += x count += 1 return int(sum_numbers / count)
查找不同年級的班級所有數據
#查找不同年級的班級所有數據 def deffentScore(grade): #不同年級對應的數據 grade_score=[] for grade_list_row in grade_list: if grade in grade_list_row[0]: grade_score.append(grade_list_row) return grade_score #查找不同年級的班級 def deffentGrade(grade): #不同年級對應的分類 grade_score=[] for grade_list_row in grade_list: if grade in grade_list_row[0] and grade_list_row[0] not in grade_score: grade_score.append(grade_list_row[0]) return grade_score
圖利用map和reduce編寫一個str2float函數,把字符串轉化成浮點數
利用map和reduce編寫一個str2float函數,把字符串‘123.456’轉換成浮點數123.456 def StrToFloat(s): l=s.split('.') return reduce(lambda x,y:int(x)+int(y)/10**len(y),l)
python移除列表空數據
#移除空數據,缺考數據,移除表格中的空格 def removeNull(alist): for i in alist: if '缺考' in i[4] or '' in i: alist.remove(i) clist=[] for i in alist: blist=[] count=0 for y in i: y=''.join(str(y).split()) count+=1 if count>2 and count<6: if '.'in y: y=StrToFloat(y) else: y=int(y) blist.append(y) clist.append(blist) return clist
flask框架使用,並從前端頁面獲取數據,后台處理返回頁面
from flask import Flask, jsonify, render_template, request, url_for
app = Flask(__name__)
def main():
@app.route("/index")
def index():
overAll()
overAllTwo()
return render_template("Base.html")
@app.route('/test1',methods=['POST'])
def testGet1():
sex = request.form.get('sex')
sex1(str(sex))
print("執行get1")
return render_template("Base.html")
if __name__ == '__main__':
main() app.run(host='127.0.0.1', port=8080, debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>課程分析</title> </head> <body> <div style="display: flex; flex-direction: column"> <div> <form action="/test" method="post"> <input type="text" name="year" > <input type="submit" value="提交" onclick="change(1)"> </form> <br> <iframe src="http://127.0.0.1:8080/yearClass" width="850px" height="400px" frameborder="1/0" name="" id="iframe-a" scrolling="no"></iframe> </div> <div> <iframe src="http://127.0.0.1:8080/overall" width="850px" height="400px" frameborder="1/0" name="" id="iframe-b" scrolling="no"></iframe> </div> <div> <iframe src="http://127.0.0.1:8080/overalltwo" width="850px" height="400px" frameborder="1/0" name="" id="iframe-c" scrolling="no"></iframe> </div> <div> <form action="/test1" method="post"> <input type="text" name="sex" > <input type="submit" value="提交" onclick="change(2)"> </form> <br> <iframe src="http://127.0.0.1:8080/sex" width="850px" height="400px" frameborder="1/0" name="" id="iframe-d" scrolling="no"></iframe> </div> </div> </body> <script> function change(e) { if(e==1){ document.getElementById('iframe-a').contentWindow.location.reload(); } else if(e==2){ document.getElementById('iframe-d').contentWindow.location.reload(); } } </script> </html>
1、項目采用的技術棧
flask框架
Numpy:矩陣計算與其它大多數框架的數據處理基礎;
Matplotlab:專業畫圖工具,話說這個單詞還是真是在Matlab之間插入了plot這個詞形成的;
PyEcharts:是一款將python與echarts結合的強大的數據可視化工具,可以展示動態圖,在線報告使用比較美觀,並且展示數據方便,鼠標懸停在圖上,即可顯示數值、標簽等。
2、系統模塊列表
柱狀圖、餅圖、折線圖、雷達圖
3、柱狀圖:每年不同班級平均成績情況
@app.route('/test',methods=['POST']) def testGet(): year = request.form.get('year') year_score=deffentScore(year) year_class=deffentGrade(year) avg_score=differentClassesEachYear(year_class,year_score) #繪圖 # //設置柱狀圖的主標題與副標題 bar = Bar("柱狀圖", "每年不同班級平均成績情況") # //添加柱狀圖的數據及配置項 bar.add("平均成績", year_class, avg_score, mark_line=["average"], mark_point=["max", "min"]) # //生成本地文件(默認為.html文件) bar.render('./templates/yearClass.html') print("執行get") return render_template("Base.html")
4、餅圖:根據不同年份總體分析平均成績情況
def overAll(): year=['2016','2017','2018'] year=['16','17','18'] year_score=[] for i in year: list1=[] for grade_list_row in grade_list: if i in grade_list_row[0]: list1.append(grade_list_row[4]) year_score.append(sum_list(list1)) # //設置主標題與副標題,標題設置居中,設置寬度為900 pie = Pie("餅狀圖", "不同年份總體平均成績情況",title_pos='center',width=900) # //加入數據,設置坐標位置為【75,50】,上方的colums選項取消顯示,顯示label標簽 pie.add("年份", year, year_score ,center=[50,50],is_legend_show=False,is_label_show=True) # //保存圖表 pie.render('./templates/overall.html')
5、折線圖
def overAllTwo(): year_score=deffentScore('16') year_class=deffentGrade('16') avg_score=differentClassesEachYear(year_class,year_score) year_score1=deffentScore('17') year_class1=deffentGrade('17') avg_score1=differentClassesEachYear(year_class1,year_score1) year_score2=deffentScore('18') year_class2=deffentGrade('18') avg_score2=differentClassesEachYear(year_class2,year_score2) class1=['班級1','班級2','班級3'] #移除最后一個元素 avg_score2.pop() line = Line("折線圖","不同年份總體平均成績情況") # //is_label_show是設置上方數據是否顯示 line.add("2016年", class1, avg_score, is_label_show=True) line.add("2017年", class1, avg_score1, is_label_show=True) line.add("2018年", class1, avg_score2, is_label_show=True) line.render('./templates/overalltwo.html')
6、雷達圖:根據不同性別學生分析成績情況
def sex1(sexName): sex_score=[] sex_class=[] score=[] for grade_list_row in grade_list: if sexName in grade_list_row[1]: sex_score.append(grade_list_row) for grade_list_row in sex_score: if grade_list_row[0] not in sex_class: sex_class.append(grade_list_row[0]) for className in sex_class: list1=[] for grade_list_row in sex_score: if className==grade_list_row[0]: list1.append(grade_list_row[4]) score.append(sum_list(list1)) score1=[] score1.append(score) print(score1,sex_class) radar = Radar("雷達圖", "不同班級男女的平均成績") schema =[('網絡1611',100), ('網絡1612',100), ('網絡1613',100), ('網絡1711',100), ('網絡1712',100), ('網絡1714',100), ( '網絡1811',100), ('網絡1814',100), ('網絡1813',100)] # //傳入坐標 radar.config(schema) # //一般默認為同一種顏色,這里為了便於區分,需要設置item的顏色 radar.add("平均成績",score1,item_color="#1C86EE") radar.render('./templates/sex.html')