溫馨提示×

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

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

python可視化數(shù)據(jù)分析圖的案例

發(fā)布時(shí)間:2020-07-20 14:13:03 來源:億速云 閱讀:370 作者:清晨 欄目:編程語言

小編給大家分享一下python可視化數(shù)據(jù)分析圖的案例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

python數(shù)據(jù)分析常用圖大集合:包含折線圖、直方圖、垂直條形圖、水平條形圖、餅圖、箱線圖、熱力圖、散點(diǎn)圖、蜘蛛圖、二元變量分布、面積圖、六邊形圖等12種常用可視化數(shù)據(jù)分析圖,后期還會(huì)不斷的收集整理,請(qǐng)關(guān)注更新!

以下默認(rèn)所有的操作都先導(dǎo)入了numpy、pandasmatplotlib、seaborn

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

一、折線圖

折線圖可以用來表示數(shù)據(jù)隨著時(shí)間變化的趨勢(shì)

x = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
y = [5, 3, 6, 20, 17, 16, 19, 30, 32, 35]

Matplotlib

plt.plot(x, y)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

Seaborn

df = pd.DataFrame({'x': x, 'y': y})
sns.lineplot(x="x", y="y", data=df)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

二、直方圖

直方圖是比較常見的視圖,它是把橫坐標(biāo)等分成了一定數(shù)量的小區(qū)間,然后在每個(gè)小區(qū)間內(nèi)用矩形條(bars)展示該區(qū)間的數(shù)值

a = np.random.randn(100)
s = pd.Series(a)

Matplotlib

plt.hist(s)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

Seaborn

sns.distplot(s, kde=False)
plt.show()
sns.distplot(s, kde=True)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

python可視化數(shù)據(jù)分析圖的案例

三、垂直條形圖

條形圖可以幫我們查看類別的特征。在條形圖中,長(zhǎng)條形的長(zhǎng)度表示類別的頻數(shù),寬度表示類別。

x = ['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5']
y = [5, 4, 8, 12, 7]

Matplotlib

plt.bar(x, y)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

Seaborn

plt.show()

python可視化數(shù)據(jù)分析圖的案例

四、水平條形圖

x = ['Cat1', 'Cat2', 'Cat3', 'Cat4', 'Cat5']
y = [5, 4, 8, 12, 7]
plt.barh(x, y)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

五、餅圖

nums = [25, 37, 33, 37, 6]
labels = ['High-school','Bachelor','Master','Ph.d', 'Others']
plt.pie(x = nums, labels=labels)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

六、箱線圖

箱線圖由五個(gè)數(shù)值點(diǎn)組成:最大值 (max)、最小值 (min)、中位數(shù) (median) 和上下四分位數(shù) (Q3, Q1)。

可以幫我們分析出數(shù)據(jù)的差異性、離散程度和異常值等。

Matplotlib

# 生成0-1之間的10*4維度數(shù)據(jù)
data=np.random.normal(size=(10,4)) 
lables = ['A','B','C','D']
# 用Matplotlib畫箱線圖
plt.boxplot(data,labels=lables)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

Seaborn

# 用Seaborn畫箱線圖
df = pd.DataFrame(data, columns=lables)
sns.boxplot(data=df)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

七、熱力圖

力圖,英文叫 heat map,是一種矩陣表示方法,其中矩陣中的元素值用顏色來代表,不同的顏色代表不同大小的值。通過顏色就能直觀地知道某個(gè)位置上數(shù)值的大小。

flights = sns.load_dataset("flights")
data=flights.pivot('year','month','passengers')
sns.heatmap(data)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

通過 seaborn 的 heatmap 函數(shù),我們可以觀察到不同年份,不同月份的乘客數(shù)量變化情況,其中顏色越淺的代表乘客數(shù)量越多

八、散點(diǎn)圖

散點(diǎn)圖的英文叫做 scatter plot,它將兩個(gè)變量的值顯示在二維坐標(biāo)中,非常適合展示兩個(gè)變量之間的關(guān)系。

N = 1000
x = np.random.randn(N)
y = np.random.randn(N)

Matplotlib

plt.scatter(x, y,marker='x')
plt.show()

python可視化數(shù)據(jù)分析圖的案例

Seaborn

df = pd.DataFrame({'x': x, 'y': y})
sns.jointplot(x="x", y="y", data=df, kind='scatter');
plt.show()

python可視化數(shù)據(jù)分析圖的案例

九、蜘蛛圖

蜘蛛圖是一種顯示一對(duì)多關(guān)系的方法,使一個(gè)變量相對(duì)于另一個(gè)變量的顯著性是清晰可見

labels=np.array([u"推進(jìn)","KDA",u"生存",u"團(tuán)戰(zhàn)",u"發(fā)育",u"輸出"])
stats=[83, 61, 95, 67, 76, 88]
# 畫圖數(shù)據(jù)準(zhǔn)備,角度、狀態(tài)值
angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
stats=np.concatenate((stats,[stats[0]]))
angles=np.concatenate((angles,[angles[0]]))
# 用Matplotlib畫蜘蛛圖
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)   
ax.plot(angles, stats, 'o-', linewidth=2)
ax.fill(angles, stats, alpha=0.25)
# 設(shè)置中文字體
font = FontProperties(fname=r"/System/Library/Fonts/PingFang.ttc", size=14)  
ax.set_thetagrids(angles * 180/np.pi, labels, FontProperties=font)
plt.show()

python可視化數(shù)據(jù)分析圖的案例

十、二元變量分布

二元變量分布可以看兩個(gè)變量之間的關(guān)系

tips = sns.load_dataset("tips")
tips.head(10)
#散點(diǎn)圖
sns.jointplot(x="total_bill", y="tip", data=tips, kind='scatter')
#核密度圖
sns.jointplot(x="total_bill", y="tip", data=tips, kind='kde')
#Hexbin圖
sns.jointplot(x="total_bill", y="tip", data=tips, kind='hex')
plt.show()

python可視化數(shù)據(jù)分析圖的案例

python可視化數(shù)據(jù)分析圖的案例

python可視化數(shù)據(jù)分析圖的案例

十一、面積圖

面積圖又稱區(qū)域圖,強(qiáng)調(diào)數(shù)量隨時(shí)間而變化的程度,也可用于引起人們對(duì)總值趨勢(shì)的注意。

堆積面積圖還可以顯示部分與整體的關(guān)系。折線圖和面積圖都可以用來幫助我們對(duì)趨勢(shì)進(jìn)行分析,當(dāng)數(shù)據(jù)集有合計(jì)關(guān)系或者你想要展示局部與整體關(guān)系的時(shí)候,使用面積圖為更好的選擇。

df = pd.DataFrame(
np.random.rand(10, 4), 
columns=['a', 'b', 'c', 'd'])
# 堆面積圖
df.plot.area()
# 面積圖
df.plot.area(stacked=False)

python可視化數(shù)據(jù)分析圖的案例

十二、六邊形圖

六邊形圖將空間中的點(diǎn)聚合成六邊形,然后根據(jù)六邊形內(nèi)部的值為這些六邊形上色。

df = pd.DataFrame(
np.random.randn(1000, 2), 
columns=['a', 'b'])
df['b'] = df['b'] + np.arange(1000)
# 關(guān)鍵字參數(shù)gridsize;它控制x方向上的六邊形數(shù)量,默認(rèn)為100,較大的gridsize意味著更多,更小的bin
df.plot.hexbin(x='a', y='b', gridsize=25)

python可視化數(shù)據(jù)分析圖的案例

看完了這篇文章,相信你對(duì)python可視化數(shù)據(jù)分析圖的案例有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

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

AI