您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何利用Python進行數(shù)據(jù)可視化,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Python主要應用于:1、Web開發(fā);2、數(shù)據(jù)科學研究;3、網(wǎng)絡爬蟲;4、嵌入式應用開發(fā);5、游戲開發(fā);6、桌面應用開發(fā)。
用 Python 簡單便捷的完成數(shù)據(jù)可視化。
其實利用 Python 可視化數(shù)據(jù)并不是很麻煩,因為 Python 中有兩個專用于可視化的庫 matplotlib 和 seaborn 能讓我們很容易的完成任務。
Matplotlib:基于Python的繪圖庫,提供完全的 2D 支持和部分 3D 圖像支持。在跨平臺和互動式環(huán)境中生成高質(zhì)量數(shù)據(jù)時,matplotlib 會很有幫助。也可以用作制作動畫。
Seaborn:該 Python 庫能夠創(chuàng)建富含信息量和美觀的統(tǒng)計圖形。Seaborn 基于 matplotlib,具有多種特性,比如內(nèi)置主題、調(diào)色板、可以可視化單變量數(shù)據(jù)、雙變量數(shù)據(jù),線性回歸數(shù)據(jù)和數(shù)據(jù)矩陣以及統(tǒng)計型時序數(shù)據(jù)等,能讓我們創(chuàng)建復雜的可視化圖形。
我們用 Python 可以做出哪些可視化圖形?
那么這里可能有人就要問了,我們?yōu)槭裁匆鰯?shù)據(jù)可視化?比如有下面這個圖表:
當然如果你把這張圖表丟給別人,他們倒是也能看懂,但無法很直觀的理解其中的信息,而且這種形式的圖表看上去也比較 low,這個時候我們?nèi)绻麚Q成直觀又美觀的可視化圖形,不僅能突顯逼格,也能讓人更容易的看懂數(shù)據(jù)。
下面我們就用上面這個簡單的數(shù)據(jù)集作為例子,展示用 Python 做出9種可視化效果,并附有相關代碼。
導入數(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)
可視化為散點圖
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ù)兩個變量比如性別(X 軸)和 BMI(Y 軸)繪出熱度圖。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何利用Python進行數(shù)據(jù)可視化”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。