Python高級數(shù)據(jù)分析之pandas和matplotlib繪圖
一、matplotlib 庫
一個用來繪圖的庫
import matplotlib.pyplot as plt
1)plt.imread(“圖片路徑”)
功能: 將圖片加載后返回一個維數(shù)組
>>> jin = plt.imread("./jin.png")
>>> jin
array([[[0.24313726, 0.24313726, 0.24705882],
...,
[0.7294118 , 0.7294118 , 0.7294118 ]]], dtype=float32)
>>> jin.shpae
(273, 411, 3)
'''
這是一個三維數(shù)組
第一層代表將圖片分成273行,
第二層代表將圖片的第一層的每一行分為411列,
第三層代表將每一像素點中的(R,G,B)
注意:有些圖片加載后最內(nèi)層有4個元素,分別是(R,G,B,A[阿爾法/透明度])
'''
2)plt.imshow(ndarray)顯示圖片
功能: 將多維數(shù)組渲染為一張圖片
>>> plt.imshow(nd) #將加載后的多維數(shù)組傳入就可以將圖片渲染出來 ''' 有的圖片加載出來之后數(shù)據(jù)范圍在0-255之間的需要將數(shù)據(jù)/255之后轉(zhuǎn)化為0-1之間的就可以和0-1的圖片進行合并操作 '''
這里就可以將圖片進行處理(反轉(zhuǎn),拉伸,改色)等操作
3)plt.imsave(ndarray)保存圖片
plt.imsave(ndarray)
默認保存到當前路徑
plt.imsave('圖片名稱.png',圖片數(shù)據(jù),cmap='gray') #保存圖片
cmap:將圖片保存為黑白圖片
二、Pandas繪圖
1.繪制簡單的線型圖
1.1)簡單的Series圖表示例 .plot()
繪制正弦曲線
# 正弦曲線 x = np.linspace(0,2*np.pi,100) # 從0 到 2π 取100份 y = np.sin(x) # y 為sin(x) 的值 s = Series(data=y,index=x) # 構(gòu)建一個Series對象 s.plot() # 使用Series的plot()方法

1.2) 兩個Series繪制的曲線可以疊加

2)簡單的DataFrame圖表示例 .plot()
繪制余弦曲線
x = np.linspace(0,2*np.pi,100) # 從0 到 2π 取100份
df = DataFrame(data={'sin':np.sin(x),'cos':np.cos(x)},index=x) #創(chuàng)建DataFrame對象
df.plot()
2.數(shù)據(jù)驅(qū)動的線型圖(分析蘋果股票)
導(dǎo)包
import numpy as np import pandas as pd from pandas import Series,DataFrame
1.讀取數(shù)據(jù)
讀取文件AAPL.csv
df = pd.read_csv('./data/AAPL.csv')
df.head()
2.檢查數(shù)據(jù)類型
df.dtypes
Date object #時間的數(shù)據(jù)類型為對象,在使用中需要做處理
Open float64
High float64
Low float64
Close float64
Adj Close float64
Volume float64
dtype: object
3.將’Date’這行數(shù)據(jù)轉(zhuǎn)換為時間數(shù)據(jù)類型
pd.to_datetime(Series對象)
功能: 將Series轉(zhuǎn)換為時間數(shù)據(jù)類型
df['Date'] # 這一列獲取出來是一個Series type(df['Date']) df['Date'] = pd.to_datetime(df['Date'])
df.dtypes
Date datetime64[ns] #此時已經(jīng)是時間類型的數(shù)據(jù)了
Open float64
High float64
Low float64
Close float64
Adj Close float64
Volume float64
dtype: object
4.將’Date’設(shè)置為行索引
df.set_index('Date',inplace=True)
'''
inplace:改變原來的變量的值
'''
5.繪制圖形,以字段Adj Close(已調(diào)整收盤價格)為數(shù)據(jù)繪制
df['Adj Close'].plot()

3.繪制簡單的柱狀圖
1) Series柱狀圖示例,kind = ‘bar’/’barh’
隨機產(chǎn)生5個數(shù),對其繪制縱向柱狀圖
'''
data: 生成之后為Y軸的數(shù)據(jù)
index: 生成之后為X軸的索引
'''
s = Series(data=np.random.randint(0,10,size=5),index=list('abcde')) #隨機產(chǎn)生5個數(shù)
s.plot(kind='bar')
隨機產(chǎn)生5個數(shù),對其繪制橫向柱狀圖
'''
data: 生成之后為Y軸的數(shù)據(jù)
index: 生成之后為X軸的索引
'''
s = Series(data=np.random.randint(0,10,size=5),index=list('abcde'))
s.plot(kind='barh')
2) DataFrame柱狀圖示例
隨機產(chǎn)生一個二維數(shù)組,并繪制縱向(kind=’bar’)柱狀圖
'''
index: 生成數(shù)據(jù)X軸的索引
columns: 特征的名稱
'''
df = DataFrame(np.random.randint(0,10,size=(8,4)),index=list('abcdefgh'),columns=list('ABCD'))
df.plot(kind='bar')
隨機產(chǎn)生一個二維數(shù)組,并繪制橫向(kind=’barh’)柱狀圖

4.繪制簡單的直方圖
直方圖
直方圖(Histogram)又稱質(zhì)量分布圖。是一種統(tǒng)計報告圖,由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況。 一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況。
s = Series(np.random.normal(loc=0,scale=5,size=10000)) # normal 正態(tài)分布 s.hist() #這里模擬出一組以0為中心,標準差為5的正態(tài)分布數(shù)據(jù), 繪制出的直方圖如下

5.繪制簡單的核密度(“ked”)圖
核心密度估計:對分布的圖進行估計核心
我們繼續(xù)使用剛剛直方圖的 Series的正態(tài)分布數(shù)據(jù)
Series(np.random.normal(loc=0,scale=5,size=10000)) s.plot(kind='kde')

直方圖一般和核密度圖常常被畫在一起,既展示出頻率,又展示出了概率
有兩組數(shù)組,我們將這兩組數(shù)據(jù)連接成為一個。
分析這組新數(shù)據(jù)
ndarr1 = np.random.normal(loc=-10,scale=5,size=5000) #第一組數(shù)據(jù) ndarr2 = np.random.normal(loc=15,scale=2,size=5000) 第二組數(shù)據(jù) ndarr = np.concatenate([ndarr1,ndarr2]) #將兩組數(shù)據(jù)合為一組 s = Series(ndarr) #生成Series對象 s.plot(kind='kde') # 核心 密度 估計 用來展示某個位置出現(xiàn)內(nèi)容的概率的估計值 s.hist(density=True) # density表示直方圖也以 密度概率的值來展示 hist用來展示某個位置出現(xiàn)內(nèi)容的實際頻率 #展示效果如下
''' s.plot(kind='kde') 展示某個位置的估計值 s.hist(density=True) 表示某個位置出現(xiàn)內(nèi)容的實際頻率 '''

6.繪制簡單的散點圖
散布圖是觀察兩個一維數(shù)據(jù)數(shù)列之間的關(guān)系的有效方法
示例數(shù)據(jù)
df = DataFrame(np.random.randint(0,150,size=(20,3)),columns=['python','math','eng'])
df #模擬一個DataFrame數(shù)據(jù),列名為'python','math','eng'
python math eng
0 141 41 55
1 37 76 96
2 61 28 135
。。。
17 124 103 83
18 86 47 44
19 35 85 85將這組數(shù)據(jù)生成散點圖
df.plot(kind='scatter',x='python',y='eng') # kind = 'scatter' , 給明標簽columns

這樣其實是沒有什么實際的意義的
現(xiàn)在我們再加一列和“python”相關(guān)的數(shù)據(jù)
np.random.randint(-10,10,1)[0] # 0-10之間 隨機取一個數(shù)
df['php'] = df['python'].map(lambda x: x*0.9+np.random.randint(-10,10,1)[0])
df
python math eng php
0 121 67 15 113.9
1 148 33 149 123.2
。。。
18 79 77 108 74.1
19 105 107 53 102.5我們從數(shù)據(jù)是很難看出來python和php有什么關(guān)系
這時我們就可以用散點圖來繪制出觀察

可以看出有python的值增大,php的值也會增大。
總結(jié)
到此這篇關(guān)于Python高級數(shù)據(jù)分析之pandas和matplotlib繪圖的文章就介紹到這了,更多相關(guān)Python pandas和matplotlib繪圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+OpenCV進行不規(guī)則多邊形ROI區(qū)域提取
ROI即感興趣區(qū)域。機器視覺、圖像處理中,從被處理的圖像以方框、圓、橢圓、不規(guī)則多邊形等方式勾勒出需要處理的區(qū)域,稱為感興趣區(qū)域,ROI。本文將利用Python和OpenCV實現(xiàn)不規(guī)則多邊形ROI區(qū)域提取,需要的可以參考一下2022-03-03
Python中的數(shù)據(jù)可視化matplotlib與繪圖庫模塊
這篇文章介紹了Python中的數(shù)據(jù)可視化matplotlib與繪圖庫模塊,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

