數(shù)據(jù)可視化
1.導(dǎo)入numpy、pandas、 pyplot和數(shù)據(jù)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
text = pd.read_csv(r'result.csv')
text.head()

2.可視化展示數(shù)據(jù)
matplotlib進(jìn)行可視化的方法

基本的可視化圖案
折線圖適用于某個(gè)屬性值隨時(shí)間變化的趨勢
散點(diǎn)圖適用于看兩個(gè)變量之間的相關(guān)性
柱狀圖適用于查看整體數(shù)據(jù)的分布情況
直方圖適用于觀察數(shù)據(jù)頻率分布情況
可視化展示泰坦尼克號數(shù)據(jù)集中男女中生存人數(shù)分布情況-柱狀圖
sex = text.groupby('Sex')['Survived'].sum()
sex.plot.bar()
plt.title('survived_count')
plt.show()

女性的存活人數(shù)高于男性的存活人數(shù),女性存活率較高。
可視化展示泰坦尼克號數(shù)據(jù)集中男女中生存人與死亡人數(shù)的比例圖-柱狀圖
text.groupby(['Sex','Survived'])['Survived'].count().unstack().plot(kind='bar',stacked='True')
plt.title('survived_count')
plt.ylabel('count')

1表示生存,0表示死亡。
可視化展示泰坦尼克號數(shù)據(jù)集中不同票價(jià)的人生存和死亡人數(shù)分布情況-折線圖
排序后折線圖
fare_sur = text.groupby(['Fare'])['Survived'].value_counts().sort_values(ascending=False)
fare_sur

fig = plt.figure(figsize=(20, 18))
fare_sur.plot(grid=True)
plt.legend()
plt.show()

排序前折線圖
fare_sur1 = text.groupby(['Fare'])['Survived'].value_counts()
fare_sur1

fig = plt.figure(figsize=(20, 18))
fare_sur1.plot(grid=True)
plt.legend()
plt.show()

可視化展示泰坦尼克號數(shù)據(jù)集中不同倉位等級的人生存和死亡人員的分布情況-柱狀圖
import seaborn as sns
sns.countplot(x="Pclass", hue="Survived", data=text)

發(fā)現(xiàn):性別與存活率有關(guān)系:女性的存活率要更高,倉位等級越高存活率越高。
可視化展示泰坦尼克號數(shù)據(jù)集中不同年齡的人生存與死亡人數(shù)分布情況
facet = sns.FacetGrid(text, hue="Survived",aspect=3)
facet.map(sns.kdeplot,'Age',shade= True)
facet.set(xlim=(0, text['Age'].max()))
facet.add_legend()

可視化展示泰坦尼克號數(shù)據(jù)集中不同倉位等級的人年齡分布情況-折線圖
text.Age[text.Pclass == 1].plot(kind='kde')
text.Age[text.Pclass == 2].plot(kind='kde')
text.Age[text.Pclass == 3].plot(kind='kde')
plt.xlabel("age")
plt.legend((1,2,3),loc="best")

發(fā)現(xiàn):女性存活率更高,倉位等級高的人存活率更高,20-30歲的人存活率更高。
DataWhale開源學(xué)習(xí)資料: