Pandas有許多能夠利用DataFrame對象數據組織特點來創建標准圖表的高級繪圖方法,本文主要介紹的是pandas中的繪圖函數。
#coding:utf-8
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from pandas import DataFrame,Series
1. 線形圖
df.plot( kind='line')
- Series 的plot 方法會以index作為X軸,畫一條線
- DataFrame 的plot 方法會以index作為X軸,給每一列繪制一條線,columns作為圖例。
#第一種創建畫布和畫布分區方法,分開創建figure和subplot對象
fig=plt.figure()
#Series 的線形圖
ax1=fig.add_subplot(2,1,1)
s=Series(np.random.rand(10).cumsum(),index=np.arange(0,100,10))
s.plot( kind='line')
plt.xlabel(u"index")
plt.title(u"Serise的線形圖")
plt.show()
#DataFrame的線形圖
ax2=fig.add_subplot(2,1,2)
df=DataFrame(np.random.rand(10,4).cumsum(0),index=np.arange(0,100,10),columns=pd.Index(['A','B','C','D'],name='Genus'))
df.plot( kind='line')
plt.xlabel(u"index")
plt.title(u"DataFrame的線形圖")
plt.show()
其中,Series.plot方法的參數
- kind :圖的類型,‘line’,'bar','barh','kde'
- label :圖例標簽
- ax :需要繪制的對象
- rot :旋轉角度
- xticks :X軸刻度值
- xlim :X軸刻度范圍
- grid :顯示網格
2. 柱狀圖
df.plot( kind='bar')
: 垂直柱狀圖
df.plot( kind='barh')
: 水平柱狀圖
df.plot( kind='bar',stacked=True)
: stacked屬性為True可以設置為堆積柱狀圖
Series的柱狀圖
#第二種創建畫布和畫布分區方法,創建figure,返回一個subplot對象
fig,axes =plt.subplots(2,1)
s=Series(np.random.rand(16),index=list('abcdefghijklmnop'))
s.plot( kind= 'bar' ,ax=axes[0]) #返回的axes的數組可指定在哪個subplot對象上畫圖
s.plot( kind= 'barh' ,ax=axes[1])
plt.show()
DataFrame的柱狀圖
- 每一行的值為一組,每一列的columns為圖例
fig,axes =plt.subplots(2,1)
df=DataFrame(np.random.rand(4,4),index=['one','two','three','four'],columns=pd.Index(['A','B','C','D'],name='Genus'))
df.plot( kind= 'bar',ax=axes[0] )
df.plot( kind= 'bar',ax=axes[1],stacked=True ) #stacked=True 可以生成堆積柱狀圖
plt.show()
3.密度圖( KDE, Kernel Density Estimate ,核密度估計 )
- 密度圖即為連續概率分布圖,將分布近似為標准混合正態分布。
df.plot( kind='kde')
fig=plt.figure()
#Series 的密度圖
fig.add_subplot(2,1,1)
s=Series(np.random.rand(50).cumsum(),index=np.arange(0,100,2))
s.plot(kind='kde')
plt.title(u"Series的密度圖")
plt.show()
#DataFrame 的密度圖,會給每一列都畫一條密度估計線,並將columns自動生成圖例
fig.add_subplot(2,1,2)
df=DataFrame(np.random.rand(10,4).cumsum(0),index=np.arange(0,100,10),columns=pd.Index(['A','B','C','D'],name='Genus'))
df.plot(kind='kde')
plt.title(u"DataFrame的密度圖")
plt.show()
4.直方圖
- 直方圖是對值頻率進行離散化顯示的柱狀圖,數據點唄拆分到離散的、間隔均勻的面元中。
df.hist( bins=10)
:bins屬性可設置柱子數量
fig=plt.figure()
#Series 的密度圖
fig.add_subplot(2,1,1)
s=Series(np.random.rand(20).cumsum(),index=np.arange(0,100,5))
s.hist( bins=10)
plt.title(u"Series的直方圖")
plt.show()
#DataFrame 的密度圖,會給每一列都畫一張直方圖,並將列名作為對應標題
fig.add_subplot(2,1,2)
df=DataFrame(np.random.rand(10,4).cumsum(0),index=np.arange(0,100,10),columns=pd.Index(['A','B','C','D'],name='Genus'))
df.hist( bins=10)
plt.show()
5.散點圖
- 散點圖是觀察兩個一維數據間關系的有效方式
plt.scatter(X,Y)
macro = pd.read_csv('macrodata.csv')
data = macro[['cpi','m1','tbilrate','unemp']]
trans_data = np.log(data).diff().dropna()
# 畫出兩個Series之間的散點圖
plt.scatter(trans_data['m1'],trans_data['unemp'])
plt.title('Change in log %s vs.log %s' % ('m1','unemp'))
plt.show()
DataFrame的散點圖矩陣
-
創建散布圖矩陣,會把DataFrame中任意兩列畫散點圖,觀察其之間的關系。
-
支持在對角線上放置各變量的直方圖或者密度圖
pd.scatter_matrix( trans_data ,diagonal = 'kde' ,color ='k' ,alpha=0.3)
# 畫出散布圖矩陣
pd.plotting.scatter_matrix( trans_data ,diagonal = 'kde' ,color ='k' ,alpha=0.3)
plt.show()