我的學習歷程 - 111-1大數據分析 - 3.數據資料視覺化2(Pandas模組) |
|
|
3.數據資料視覺化2(Pandas模組)(1)#範例3-9:印出四位學生的三科成績 #此作者為1101AD001 詹子嫻 111/11/20 import pandas as pd #數據資料表,採用三個串列List(coulumn=course,record_name=name, record_data = score) score = [[75,85,95],[90,90,90],[65,55,60],[95,65,90]] name = ['tom','Vivian','peter','jolin'] course = ['chinese','english','math'] #設定dataFrame設定資料結構 df = pd.DataFrame(score, index = name, columns = course) #繪line線條圖 df.plot(kind='line',title='score of students') #繪bar柱狀圖 df.plot(kind='bar',title='score of students') (2) #範例3-7:統計同一州的人數柱狀圖顯示(state vs name)Bar plot with group by #此作這為 1101AD001 詹子嫻 #指令:df.groupby('state')['name'].nunique().plot(kind='bar') import pandas as pd df = pd.DataFrame({ 'name':['john','mary','peter','jeff','bill','lisa','jose'], 'age':[23,78,22,19,45,33,20], 'gender':['M','F','M','M','M','F','M'], 'state':['california','dc','california','dc','california','texas','texas'], 'num_children':[2,0,0,3,2,1,4], 'num_pets':[5,1,0,5,2,2,3] }) df.groupby('state')['name'].nunique().plot(kind='bar') (3) #3-6:如何把圖形存檔成p3-6.png #此作者為 1101AD001 詹子嫻 #指令:plt.savefig('p3-6.png') #注意:這是matplotlib.pyplot函數庫指令,故必須先import import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'name':['john','mary','peter','jeff','bill','lisa','jose'], 'age':[23,78,22,19,45,33,20], 'gender':['M','F','M','M','M','F','M'], 'state':['california','dc','california','dc','california','texas','texas'], 'num_children':[2,0,0,3,2,1,4], 'num_pets':[5,1,0,5,2,2,3] }) # gca 代表取得目figure前坐標軸 axis(gca = get current figure) ax1 = plt.gca() #顯示每人寵物數量(座標軸 = ax1) df.plot(ax=ax1, kind='line',x='name',y='num_pets',color='red') #顯示每人的小孩數量(座標軸 = ax1) df.plot(ax=ax1, kind='line',x='name', y='num_children',color='green') #兩個圖組,一起存檔 #plt.show() plt.savefig('p3-6.png') plt.savefig('p3-6.png')
|
|
中華科技大學數位化學習歷程 - 意見反應 |