利用Python進(jìn)行數(shù)據(jù)可視化常見的9種方法!超實(shí)用!
前言
如同藝術(shù)家們用繪畫讓人們更貼切的感知世界,數(shù)據(jù)可視化也能讓人們更直觀的傳遞數(shù)據(jù)所要表達(dá)的信息。
我們今天就分享一下如何用 Python 簡單便捷的完成數(shù)據(jù)可視化。
其實(shí)利用 Python 可視化數(shù)據(jù)并不是很麻煩,因?yàn)?Python 中有兩個(gè)專用于可視化的庫 matplotlib 和 seaborn 能讓我們很容易的完成任務(wù)。
- Matplotlib:基于Python的繪圖庫,提供完全的 2D 支持和部分 3D 圖像支持。在跨平臺和互動式環(huán)境中生成高質(zhì)量數(shù)據(jù)時(shí),matplotlib 會很有幫助。也可以用作制作動畫。
- Seaborn:該 Python 庫能夠創(chuàng)建富含信息量和美觀的統(tǒng)計(jì)圖形。Seaborn 基于 matplotlib,具有多種特性,比如內(nèi)置主題、調(diào)色板、可以可視化單變量數(shù)據(jù)、雙變量數(shù)據(jù),線性回歸數(shù)據(jù)和數(shù)據(jù)矩陣以及統(tǒng)計(jì)型時(shí)序數(shù)據(jù)等,能讓我們創(chuàng)建復(fù)雜的可視化圖形。
我們用 Python 可以做出哪些可視化圖形?
那么這里可能有人就要問了,我們?yōu)槭裁匆鰯?shù)據(jù)可視化?比如有下面這個(gè)圖表:
當(dāng)然如果你把這張圖表丟給別人,他們倒是也能看懂,但無法很直觀的理解其中的信息,而且這種形式的圖表看上去也比較 low,這個(gè)時(shí)候我們?nèi)绻麚Q成直觀又美觀的可視化圖形,不僅能突顯逼格,也能讓人更容易的看懂?dāng)?shù)據(jù)。
下面我們就用上面這個(gè)簡單的數(shù)據(jù)集作為例子,展示用 Python 做出9種可視化效果,并附有相關(guān)代碼。
導(dǎo)入數(shù)據(jù)集
import matplotlib.pyplot as plt import pandas as pd df=pd.read_excel("E:/First.xlsx", "Sheet1")
可視化為直方圖
fig=plt.figure() #Plots in matplotlib reside within a figure object, use plt.figure to create new figure #Create one or more subplots using add_subplot, because you can't create blank figure ax = fig.add_subplot(1,1,1) #Variable ax.hist(df['Age'],bins = 7) # Here you can play with number of bins Labels and Tit plt.title('Age distribution') plt.xlabel('Age') plt.ylabel('#Employee') plt.show()
可視化為箱線圖
import matplotlib.pyplot as plt import pandas as pd fig=plt.figure() ax = fig.add_subplot(1,1,1) #Variable ax.boxplot(df['Age']) plt.show()
可視化為小提琴圖
import seaborn as sns sns.violinplot(df['Age'], df['Gender']) #Variable Plot sns.despine()
可視化為條形圖
var = df.groupby('Gender').Sales.sum() #grouped sum of sales at Gender level fig = plt.figure() ax1 = fig.add_subplot(1,1,1) ax1.set_xlabel('Gender') ax1.set_ylabel('Sum of Sales') ax1.set_title("Gender wise Sum of Sales") var.plot(kind='bar')
可視化為折線圖
var = df.groupby('BMI').Sales.sum() fig = plt.figure() ax1 = fig.add_subplot(1,1,1) ax1.set_xlabel('BMI') ax1.set_ylabel('Sum of Sales') ax1.set_title("BMI wise Sum of Sales") var.plot(kind='line')
可視化為堆疊柱狀圖
var = df.groupby(['BMI','Gender']).Sales.sum() var.unstack().plot(kind='bar',stacked=True, color=['red','blue'], grid=False)
可視化為散點(diǎn)圖
fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(df['Age'],df['Sales']) #You can also add more variables here to represent color and size. plt.show()
可視化為泡泡圖
fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.scatter(df['Age'],df['Sales'], s=df['Income']) # Added third variable income as size of the bubble plt.show()
可視化為餅狀圖
var=df.groupby(['Gender']).sum().stack() temp=var.unstack() type(temp) x_list = temp['Sales'] label_list = temp.index pyplot.axis("equal") #The pie chart is oval by default. To make it a circle use pyplot.axis("equal") #To show the percentage of each pie slice, pass an output format to the autopctparameter plt.pie(x_list,labels=label_list,autopct="%1.1f%%") plt.title("Pastafarianism expenses") plt.show()
可視化為熱度圖
import numpy as np #Generate a random number, you can refer your data values also data = np.random.rand(4,2) rows = list('1234') #rows categories columns = list('MF') #column categories fig,ax=plt.subplots() #Advance color controls ax.pcolor(data,cmap=plt.cm.Reds,edgecolors='k') ax.set_xticks(np.arange(0,2)+0.5) ax.set_yticks(np.arange(0,4)+0.5) # Here we position the tick labels for x and y axis ax.xaxis.tick_bottom() ax.yaxis.tick_left() #Values against each labels ax.set_xticklabels(columns,minor=False,fontsize=20) ax.set_yticklabels(rows,minor=False,fontsize=20) plt.show()
你也可以自己試著根據(jù)兩個(gè)變量比如性別(X 軸)和 BMI(Y 軸)繪出熱度圖。
結(jié)語
本文我們分享了如何利用 Python 及 matplotlib 和 seaborn 庫制作出多種多樣的可視化圖形。通過上面的例子,我們應(yīng)該可以感受到利用可視化能多么美麗的展示數(shù)據(jù)。而且和其它語言相比,使用 Python 進(jìn)行可視化更容易簡便一些。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
參考資料:
https://www.analyticsvidhya.com/blog/2015/05/data-visualization-python/
- Python數(shù)據(jù)可視化 pyecharts實(shí)現(xiàn)各種統(tǒng)計(jì)圖表過程詳解
- 利用Python代碼實(shí)現(xiàn)數(shù)據(jù)可視化的5種方法詳解
- Python數(shù)據(jù)可視化庫seaborn的使用總結(jié)
- Python數(shù)據(jù)可視化:箱線圖多種庫畫法
- Python數(shù)據(jù)可視化之畫圖
- Python數(shù)據(jù)可視化:餅狀圖的實(shí)例講解
- Python數(shù)據(jù)可視化:泊松分布詳解
- Python數(shù)據(jù)可視化:冪律分布實(shí)例詳解
- python實(shí)現(xiàn)股票歷史數(shù)據(jù)可視化分析案例
- Python?"手繪風(fēng)格"數(shù)據(jù)可視化方法實(shí)例匯總
相關(guān)文章
用十張圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制(附代碼)
這篇文章主要介紹了用十張圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制(附代碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02json-server?如何快速搭建REST?API?服務(wù)器
json-server 是一個(gè)非常流行的開源工具,用于快速搭建一個(gè)完整的 REST API 服務(wù)器,它使用 JSON 文件作為數(shù)據(jù)源,通過簡單的配置即可模擬復(fù)雜的服務(wù)器功能,這篇文章主要介紹了json-server如何快速搭建REST API服務(wù)器,需要的朋友可以參考下2017-10-10在PyCharm中遇到pip安裝 失敗問題及解決方案(pip失效時(shí)的解決方案)
這篇文章主要介紹了在PyCharm中遇到pip安裝失敗問題及解決方案(pip失效時(shí)的解決方案),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03python簡單獲取本機(jī)計(jì)算機(jī)名和IP地址的方法
這篇文章主要介紹了python簡單獲取本機(jī)計(jì)算機(jī)名和IP地址的方法,涉及Python中socket模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06python實(shí)現(xiàn)五子棋游戲(pygame版)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)五子棋游戲,pygame版五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01