python數(shù)據(jù)分析常用圖大集合:包含折線(xiàn)圖、直方圖、垂直條形圖、水平條形圖、餅圖、箱線(xiàn)圖、熱力圖、散點(diǎn)圖、蜘蛛圖、二元變量分布、面積圖、六邊形圖等12種常用可視化數(shù)據(jù)分析圖,后期還會(huì)不斷的收集整理,請(qǐng)關(guān)注更新!
以下默認(rèn)所有的操作都先導(dǎo)入了numpy、pandas、matplotlib、seaborn
import?numpy?as?np
import?pandas?as?pd
import?matplotlib.pyplot?as?plt
import?seaborn?as?sns
一、折線(xiàn)圖
折線(xiàn)圖可以用來(lái)表示數(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()

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

二、直方圖
直方圖是比較常見(jiàn)的視圖,它是把橫坐標(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()

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


三、垂直條形圖
條形圖可以幫我們查看類(lèi)別的特征。在條形圖中,長(zhǎng)條形的長(zhǎng)度表示類(lèi)別的頻數(shù),寬度表示類(lèi)別。
x?=?['Cat1',?'Cat2',?'Cat3',?'Cat4',?'Cat5']
y?=?[5,?4,?8,?12,?7]
Matplotlib
plt.bar(x,?y)
plt.show()

Seaborn
1plt.show()

四、水平條形圖
x?=?['Cat1',?'Cat2',?'Cat3',?'Cat4',?'Cat5']
y?=?[5,?4,?8,?12,?7]
plt.barh(x,?y)
plt.show()

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

六、箱線(xiàn)圖
箱線(xiàn)圖由五個(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畫(huà)箱線(xiàn)圖
plt.boxplot(data,labels=lables)
plt.show()

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

七、熱力圖
力圖,英文叫 heat map,是一種矩陣表示方法,其中矩陣中的元素值用顏色來(lái)代表,不同的顏色代表不同大小的值。通過(guò)顏色就能直觀地知道某個(gè)位置上數(shù)值的大小。
flights?=?sns.load_dataset("flights")
data=flights.pivot('year','month','passengers')
sns.heatmap(data)
plt.show()

通過(guò) 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()

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

九、蜘蛛圖
蜘蛛圖是一種顯示一對(duì)多關(guān)系的方法,使一個(gè)變量相對(duì)于另一個(gè)變量的顯著性是清晰可見(jiàn)
labels=np.array([u"推進(jìn)","KDA",u"生存",u"團(tuán)戰(zhàn)",u"發(fā)育",u"輸出"])
stats=[83,?61,?95,?67,?76,?88]
#?畫(huà)圖數(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畫(huà)蜘蛛圖
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()

十、二元變量分布
二元變量分布可以看兩個(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()



十一、面積圖
面積圖又稱(chēng)區(qū)域圖,強(qiáng)調(diào)數(shù)量隨時(shí)間而變化的程度,也可用于引起人們對(duì)總值趨勢(shì)的注意。
堆積面積圖還可以顯示部分與整體的關(guān)系。折線(xiàn)圖和面積圖都可以用來(lái)幫助我們對(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)

十二、六邊形圖
六邊形圖將空間中的點(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)
原文至:https://www.py.cn/toutiao/16894.html