說到數據可視化繪圖,我們先想到的應該是matplotlib庫,可以對其中的
axes
對象等調用不同的繪圖方法(如axes.plot()
)。
作爲數據分析用的pandas庫提供了Series
DataFrame
等類型的對象,我們也可以調用上述對象來繪圖(如Series.plot()
)。
本文將介紹這個庫之間的關聯,以及他們畫圖之間的異同。
1.關聯
- Pandas庫提供了
Series
DataFrame
等類型的對象,可以在matplotlib畫圖中作爲數據來源放入參數中,如axes.plot(Series)
- 可以對Pandas的對象調用畫圖方法,如
Series.plot(kind='line')
,但說到底還是與matplotlib有關的,是pandas自動幫你生成了axes
對象。 - 我們可以把Pandas畫出來的圖回傳給指定的
axes
對象,如Series.plot(kind='line', ax=ax1)
就把這張折線圖畫在了ax1
對象上。
下面我將用matplotlib繪圖和Pandas繪圖兩種方法畫出同樣的一張多子圖圖表
matplotlib方法:
import matplotlib.pyplot as plt
from pandas import Series
data = Series([1.47,1.62,1.78,1.94,2.38,2.60],index=['2012','2013','2014','2015','2016','2017'])
#實例化fig1對象
fig1=plt.figure(figsize=(8,8),facecolor='w')
#實例化ax對象
ax1 = fig1.add_subplot(2,2,1)
ax2 = fig1.add_subplot(2,2,2)
ax3 = fig1.add_subplot(2,1,2)
#ax對象畫圖
#畫ax1
ax1.plot(data)#注意對象名是ax1,它是matplotlib中的axes對象類型
ax1.set_title('line chart')
ax1.set_xlabel('Year')
ax1.set_ylabel('Income')
#畫ax2
ax2.boxplot(data)
ax2.set_xticks([])
ax2.set_title('box plot')
ax2.set_xlabel('2012~2017')
ax2.set_ylabel('Income')
#畫ax3
ax3.bar(data.index, data)
ax3.set_title('bar chart')
ax3.set_xlabel('Year')
ax3.set_ylabel('Income')
#設置fig1標題
fig1.suptitle('Using matplotlib')
Pandas方法:
import matplotlib.pyplot as plt
from pandas import Series
data = Series([1.47,1.62,1.78,1.94,2.38,2.60],index=['2012','2013','2014','2015','2016','2017'])
#實例化fig1對象
fig2=plt.figure(figsize=(8,8),facecolor='w')
#實例化ax對象
ax4 = fig2.add_subplot(2,2,1)
ax5 = fig2.add_subplot(2,2,2)
ax6 = fig2.add_subplot(2,1,2)
#用pandas繪圖並將圖像回傳給ax對象
#畫ax4
data.plot(kind='line',ax=ax4)#注意對象名是data,它是Pandas中的Series對象類型
ax4.set_title('line chart')
ax4.set_xlabel('Year')
ax4.set_ylabel('Income')
#畫ax5
data.plot(kind='box',ax=ax5)
ax5.set_xticks([])
ax5.set_title('box plot')
ax5.set_xlabel('2012~2017')
ax5.set_ylabel('Income')
#畫ax6
data.plot(kind='bar',ax=ax6)
ax6.set_title('bar chart')
ax6.set_xlabel('Year')
ax6.set_ylabel('Income')
#設置fig2標題
fig2.suptitle('Using Pandas')
由上面的兩個例子可以看出調用Pandas繪圖和調用matplotlib繪圖很多時候可以達到相同的效果
2.不同點
仔細分析畫柱狀圖的兩個語句ax3.bar(data.index, data)
和data.plot(kind='bar',ax=ax6)
會發現在調用ax3
畫圖的時候bar()
方法裏面有兩個參量data.index
和data
,分別是這張圖的x軸類別和y軸高度。
而用pandas畫圖的時候,我們只用把data
對象放在前面,它就能自動識別出data
裏面的index
和value
。
從上面也可以看出來,對於用Series
DataFrame
這些用Pandas對象封裝的多維的數據,調用Pandas繪圖會有更方便和更好的支持。
下面再舉一例:
我們有一個DataFrame類型的人員信息表格,每一行包含一個人的性別,收入等信息。現在我要按照性別分類,畫出女性的收入的箱線圖和男性收入的箱線圖:
import pandas as pd
df = pd.read_csv('Data/bank-data.csv',index_col=0)
df
age | sex | region | income | married | children | car | save_act | current_act | mortgage | pep | |
---|---|---|---|---|---|---|---|---|---|---|---|
id | |||||||||||
ID12101 | 48 | FEMALE | INNER_CITY | 17546.00 | NO | 1 | NO | NO | NO | NO | YES |
ID12102 | 40 | MALE | TOWN | 30085.10 | YES | 3 | YES | NO | YES | YES | NO |
ID12103 | 51 | FEMALE | INNER_CITY | 16575.40 | YES | 0 | YES | YES | YES | NO | NO |
ID12104 | 23 | FEMALE | TOWN | 20375.40 | YES | 3 | NO | NO | YES | NO | NO |
ID12105 | 57 | FEMALE | RURAL | 50576.30 | YES | 0 | NO | YES | NO | NO | NO |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
ID12696 | 61 | FEMALE | INNER_CITY | 47025.00 | NO | 2 | YES | YES | YES | YES | NO |
ID12697 | 30 | FEMALE | INNER_CITY | 9672.25 | YES | 0 | YES | YES | YES | NO | NO |
ID12698 | 31 | FEMALE | TOWN | 15976.30 | YES | 0 | YES | YES | NO | NO | YES |
ID12699 | 29 | MALE | INNER_CITY | 14711.80 | YES | 0 | NO | YES | NO | YES | NO |
ID12700 | 38 | MALE | TOWN | 26671.60 | NO | 0 | YES | NO | YES | YES | YES |
600 rows × 11 columns
如果要用matplotlib繪圖的話,就必須對DataFrame進行有條件的篩選切片,選出來所有女性的income
列和男性的income
列,然後再畫箱線圖:
fig7 = plt.figure(figsize=(6,6),facecolor='w')
ax7 = plt.axes()
ax7.boxplot( (df.loc[df['sex']=='FEMALE', 'income'], df.loc[df['sex']=='MALE', 'income']),
labels=('FEMALE','MALE'))
ax7.grid()
如果使用Pandas繪圖的話,只需以df
作爲對象放在前面,調用boxplot()
方法,並將'income',by='sex'
填入參數裏即可,相當於是Pandas自動幫你切片出income
這一列,並以sex
歸類聚合了,比調用matplotlib方便不少,也直觀很多:
fig8 = plt.figure(figsize=(6,6),facecolor='w')
ax8 = plt.axes()
df.boxplot('income', by='sex',ax=ax8)#對於多類別,需要聚合的數據,用pandas調用繪圖要比axex調用繪圖來得方便
3.總結
在大多數情況下我們可以選擇Pandas繪圖和matplotlib繪圖去實現同樣的功能,他們之間也有千絲萬縷的聯繫。
在一些用Pandas封裝的多維的數據(如DataFrame
),調用Pandas繪圖會有更方便和更好的支持,免去了手動切片、聚合數據等麻煩。
覺得有用的話,不要吝惜評論點贊分享哦,希望大家多多包涵,有任何問題歡迎指正、討論。
本文基於CC-BY-SA 4.0協議,歡迎轉載
(博客看累了?去我的B站瞧一瞧?)