1. 程式人生 > >Python資料科學入門(matplotlib)筆記04

Python資料科學入門(matplotlib)筆記04

Python資料科學入門學習筆記——Matloptlib

什麼是matloptlib
matloptilib是一個Python包,用於2D繪圖,3D繪圖也可以安裝額外的包,強大流行,有很多擴充套件。

1.Matplotlib Architecture

  • Backend:主要處理把影象顯示到哪裡和畫到哪裡?
  • Artist:影象顯示成什麼樣? 大小位置等。
  • Scripting:pyplot,Python語法和API

一、matplotlib 簡單繪圖示例

import numpy as np
import matplotlib.pyplot as plt
a = [1,2,3]
b = [4
,5,6] # 如果不想每次都呼叫 plt.show() 來顯示影象 # 使用 %matplotlib inline matplotlib 內建的魔法函式 #每次呼叫plot() 就會預設顯示影象,不用再呼叫show() plt.plot(a,b,'r--') plt.show()

圖一

c = [2,5,8]
d = [9,4,1]
plt.plot(a,b,'r*',c,d,'b--')

圖二

t = np.arange(0.0,2.0,0.1)
s = np.sin(t*np.pi)
plt.plot(t,s,'y--',label='AA')
plt.plot(t*2,s,'r*',label='BB'
) plt.xlabel('t') plt.ylabel('s') plt.title('Demo') plt.legend() # 顯示示例label

圖三

二、subplot 子圖繪製

import numpy as np
import matplotlib.pyplot as plt
t1 = np.arange(0.0,2.0,0.1)
t2 = t1 * 2
s = np.sin(t*np.pi)
plt.subplot(2,1,1)  # 表示兩行一列 圖1
plt.plot(t1,s,'y--',label='AA')

plt.subplot(212)  # 不用逗號也可以,表示兩行一列 圖二
plt.plot(t2,s,'r*',label='BB') plt.xlabel('t2') plt.ylabel('s')

這裡寫圖片描述

subplots

figure,ax = plt.subplots(2,2)   # 表示兩行兩列的畫布
ax[0][0].plot(t1,s)
ax[0][1].plot(t2,s)

這裡寫圖片描述

三、Pandas繪圖之Series

import numpy as np
import pandas as pd
from pandas import Series
import matplotlib.pyplot as plt
# cumsum() 表示累加的和
s1 = Series(np.random.randn(10)).cumsum()
s2 = Series(np.random.randn(10)).cumsum()
# bar 表示 條形圖 預設 line 線形圖 
# gird 是否顯示 格子
s1.plot(kind='line',grid=True,label='AAA',title='Demo',style='--')  
s2.plot(label='BBB')

plt.legend()  # 顯示圖例
plt.show()

圖一

sublots 子圖繪製

fig, ax = plt.subplots(2,1)
ax[0].plot(s1)  # 方法一
s2.plot(ax=ax[1],kind='bar')   # 方法二

這裡寫圖片描述

四、Pandas繪圖之DataFrame

import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
df =  DataFrame(
    np.random.randint(1,10,40).reshape(10,4),
    columns=['A','B','C','D']   
)
df.head()
A B C D
0 3 9 8 5
1 4 9 3 5
2 6 6 6 5
3 6 5 2 6
4 3 6 5 6
# barh 橫向 條形圖
# stacked 是否堆疊
df.plot(kind='bar',stacked='True')

圖一

# area 填充的圖
# 預設對行進行 繪圖 可通過 df.T.plot() 轉置後改為對'列'繪圖
df.plot(kind='area')

圖二

# iloc[5] 第五行
df.iloc[5].plot()

圖三

# 一行行畫圖
for i in df.index:
    df.iloc[i].plot(label=str(i))
plt.legend()
plt.show()

圖四

五、直方圖

import numpy as np
import pandas as pd
from pandas import Series
import matplotlib.pyplot as plt
s = Series(np.random.randn(1000))
# 頻數直方圖 分佈圖  下圖的橫軸表示 某個範圍內的頻數
# bins 表示分割槽數目  
re = plt.hist(s,rwidth=0.9,bins=20,color='r')  

圖一

print(type(re))
print('--------------------')
print(len(re))  # 長度
print('--------------------')
print(re[0])    # 頻數
print('--------------------')
print(re[1])   # 範圍
print('--------------------')
print(re[2])   # 資料型別 和 (區間)數目
<class 'tuple'>
--------------------
3
--------------------
[  1.   0.   4.   8.  15.  36.  45.  74. 128. 128. 125. 134. 115.  77.
  43.  36.  15.  11.   4.   1.]
--------------------
[-3.66698017 -3.31914432 -2.97130846 -2.62347261 -2.27563675 -1.92780089
 -1.57996504 -1.23212918 -0.88429333 -0.53645747 -0.18862162  0.15921424
  0.50705009  0.85488595  1.20272181  1.55055766  1.89839352  2.24622937
  2.59406523  2.94190108  3.28973694]
--------------------
<a list of 20 Patch objects>
# 資料直接繪圖   對比上圖
plt.plot(s)

圖二

# 密度圖
s.plot(kind='kde')

圖三