溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

發(fā)布時間:2021-11-25 14:00:12 來源:億速云 閱讀:224 作者:小新 欄目:大數(shù)據(jù)

小編給大家分享一下Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

數(shù)據(jù)可視化可以讓我們很直觀的發(fā)現(xiàn)數(shù)據(jù)中隱藏的規(guī)律,察覺到變量之間的互動關(guān)系,可以幫助我們更好的給他人解釋現(xiàn)象,做到一圖勝千文的說明效果。

常見的數(shù)據(jù)可視化庫有:

  • matplotlib 是最常見的2維庫,可以算作可視化的必備技能庫,由于matplotlib是比較底層的庫,api很多,代碼學(xué)起來不太容易。

  • seaborn 是建構(gòu)于matplotlib基礎(chǔ)上,能滿足絕大多數(shù)可視化需求。更特殊的需求還是需要學(xué)習(xí)matplotlib

  • pyecharts 上面的兩個庫都是靜態(tài)的可視化庫,而pyecharts有很好的web兼容性,可以做到可視化的動態(tài)效果。

但是在數(shù)據(jù)科學(xué)中,幾乎都離不開pandas數(shù)據(jù)分析庫,而pandas可以做

  • 數(shù)據(jù)采集 如何批量采集網(wǎng)頁表格數(shù)據(jù)?

  • 數(shù)據(jù)讀取 pd.read_csv/pd.read_excel

  • 數(shù)據(jù)清洗(預(yù)處理) 理解pandas中的apply和map的作用和異同

  • 可視化,兼容matplotlib語法(今天重點)

在本文我們可以學(xué)到用pandas做

  • 導(dǎo)入數(shù)據(jù)

  • 繪制最簡單的圖plot()

  • 多個y的繪制圖

  • 折線圖、條形圖、餅形圖和散點圖繪制

  • 統(tǒng)計信息繪圖

  • 箱型圖

  • 軸坐標刻度

  • plot()更多精細化參數(shù)

  • 可視化結(jié)果輸出保存

  •  

準備工作

如果你之前沒有學(xué)過pandas和matpltolib,我們先安裝好這幾個庫

!pip3 install numpy
!pip3 install pandas
!pip3 install matplotlib

已經(jīng)安裝好,現(xiàn)在我們導(dǎo)入這幾個要用到的庫。使用的是倫敦天氣數(shù)據(jù),一開始我們只有12個月的小數(shù)據(jù)作為例子

#jupyter notebook中需要加這行代碼
%matplotlib inlineimport matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#讀取天氣數(shù)據(jù)df = pd.read_csv('data/london2018.csv')
df

plot最簡單的圖

選擇Month作為橫坐標,Tmax作為縱坐標,繪圖。

大家注意下面兩種寫法

#寫法df.plot(x='Month', y='Tmax')plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

  • 橫坐標軸參數(shù)x傳入的是df中的列名Month

  • 縱坐標軸參數(shù)y傳入的是df中的列名Tmax

折線圖

上面的圖就是折線圖,折線圖畫法有三種

  • df.plot(x='Month', y='Tmax')

  • df.plot(x='Month', y='Tmax', kind='line')

  • df.plot.line(x='Month', y='Tmax')

df.plot.line(x='Month', y='Tmax')
plt.show()
#grid繪制格線
df.plot(x='Month', y='Tmax', kind='line', grid=True)
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

多個y值

上面的折線圖中只有一條線, 如何將多個y繪制到一個圖中

比如Tmax, Tmin

df.plot(x='Month', y=['Tmax', 'Tmin'])
plt.show()

條形圖

df.plot(x='Month',
        y='Rain',
        kind='bar')
#同樣還可以這樣畫#df.plot.bar(x='Month', y='Rain')
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

水平條形圖

bar換為barh,就可以將條形圖變?yōu)樗綏l形圖

df.plot(x='Month',
        y='Rain',
        kind='barh')
#同樣還可以這樣畫#df.plot.bar(x='Month', y='Rain')
plt.show()

多個變量的條形圖

df.plot(kind='bar',
        x = 'Month',
        y=['Tmax', 'Tmin'])
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

散點圖

df.plot(kind='scatter',
        x = 'Month',
        y = 'Sun')
plt.show()

餅形圖

df.plot(kind='pie', y='Sun')
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

上圖繪制有兩個小問題:

  • legend圖例不應(yīng)該顯示

  • 月份的顯示用數(shù)字不太正規(guī)

df.index = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
df.plot(kind='pie', y = 'Sun', legend=False)
plt.show()

更多數(shù)據(jù)

一開頭的數(shù)據(jù)只有12條記錄(12個月)的數(shù)據(jù),現(xiàn)在我們用更大的倫敦天氣數(shù)據(jù)

import pandas as pd
df2 = pd.read_csv('data/londonweather.csv')
df2.head()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

df2.Rain.describe()
count    748.000000
mean      50.408957
std       29.721493
min        0.30000025
%       27.80000050%       46.10000075%       68.800000max      174.800000Name: Rain, dtype: float64

上面一共有748條記錄, 即62年的記錄。

箱型圖

df2.plot.box(y='Rain')
#df2.plot(y='Rain', kind='box')
plt.show()

直方圖

df2.plot(y='Rain', kind='hist')
#df2.plot.hist(y='Rain')
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

縱坐標的刻度可以通過bins設(shè)置

df2.plot(y='Rain', kind='hist', bins=[0,25,50,75,100,125,150,175, 200])
#df2.plot.hist(y='Rain')
plt.show()

多圖并存

df.plot(kind='line',
        y=['Tmax', 'Tmin', 'Rain', 'Sun'], #4個變量可視化
        subplots=True,   #多子圖并存        layout=(2, 2),   #子圖排列2行2列
				figsize=(20, 10)) #圖布的尺寸
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

df.plot(kind='bar',
        y=['Tmax', 'Tmin', 'Rain', 'Sun'], #4個變量可視化
        subplots=True,   #多子圖并存        layout=(2, 2),   #子圖排列2行2列
				figsize=(20, 10)) #圖布的尺寸
plt.show()

Python pandas數(shù)據(jù)分析庫:數(shù)據(jù)可視化練習(xí)

加標題

給可視化起個標題

df.plot(kind='bar',
        y=['Tmax', 'Tmin'], #2個變量可視化
        subplots=True,   #多子圖并存        layout=(1, 2),   #子圖排列1行2列
				figsize=(20, 5),#圖布的尺寸
        title='The Weather of London')  #標題
plt.show()

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

保存結(jié)果

可視化的結(jié)果可以存儲為圖片文件

df.plot(kind='pie', y='Rain', legend=False, figsize=(10, 5), title='Pie of Weather in London')
plt.savefig('img/pie.png')
plt.show()

Python pandas數(shù)據(jù)分析庫:數(shù)據(jù)可視化練習(xí)

df.plot更多參數(shù)

df.plot(x, y, kind, figsize, title, grid, legend, style)

  • x 只有dataframe對象時,x可用。橫坐標

  • y 同上,縱坐標變量

  • kind 可視化圖的種類,如line,hist, bar, barh, pie, kde, scatter

  • figsize 畫布尺寸

  • title 標題

  • grid 是否顯示格子線條

  • legend 是否顯示圖例

  • style 圖的風(fēng)格

查看plot參數(shù)可以使用help

import pandas as pd
help(pd.DataFrame.plot)

Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化

看完了這篇文章,相信你對“Python中pandas數(shù)據(jù)分析庫如何實現(xiàn)數(shù)據(jù)可視化”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI