#範例3-5:如何把兩組線條同時畫在同一張圖上 #從DataFrame資料裡面取出num_children vs num_pets來繪圖 #原理:先取得目前顯示圖figure的座標軸 ax1 = plt.gca() #注意:這個取得座標軸ax1的方法,是在matplotlib.pyplot函數庫裡面,必須先import #然後圖1:df.plot(ax=ax1....) #然後圖2:df.plot(ax=ax1....) #最後用:matplotlib的plt.plot()繪出 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()