pandas探索你的數(shù)據(jù)實現(xiàn)可視化示例詳解
探索泰坦尼克災(zāi)難數(shù)據(jù)
在數(shù)據(jù)分析的旅程中,可視化數(shù)據(jù)是一個不可或缺的步驟,它可以幫助我們更好地理解數(shù)據(jù)、發(fā)現(xiàn)趨勢和關(guān)聯(lián)。在本篇博客中,我們將使用 Python 中的 pandas、matplotlib 和 seaborn 庫,來探索泰坦尼克災(zāi)難的數(shù)據(jù)集,以圖表形式呈現(xiàn)數(shù)據(jù)的各種方面。
步驟1 導(dǎo)入必要的庫
首先,我們導(dǎo)入了一些必要的庫,包括 pandas 用于數(shù)據(jù)操作,matplotlib 和 seaborn 用于數(shù)據(jù)可視化,以及 numpy 用于數(shù)值計算。這些庫將在整個分析過程中發(fā)揮關(guān)鍵作用。
# 運行以下代碼 import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np %matplotlib inline
步驟2 從以下地址導(dǎo)入數(shù)據(jù)
在這一步,我們準備導(dǎo)入泰坦尼克災(zāi)難的數(shù)據(jù),這些數(shù)據(jù)存儲在名為 "train.csv" 的文件中。數(shù)據(jù)導(dǎo)入是數(shù)據(jù)分析的第一步,讓我們能夠開始探索和分析數(shù)據(jù)。
# 運行以下代碼 path7 = 'exercise_data/train.csv' # train.csv
步驟3 將數(shù)據(jù)框命名為titanic
我們成功導(dǎo)入數(shù)據(jù)后,將數(shù)據(jù)框命名為 "titanic" 并顯示前幾行數(shù)據(jù),以便查看數(shù)據(jù)的結(jié)構(gòu)和內(nèi)容。
# 運行以下代碼 titanic = pd.read_csv(path7) titanic.head()

步驟4 將PassengerId設(shè)置為索引
在這一步,我們將 "PassengerId" 列設(shè)置為數(shù)據(jù)的索引。這可以幫助我們更容易地訪問和操作數(shù)據(jù)。
# 運行以下代碼
titanic.set_index('PassengerId').head()
步驟5 繪制一個展示男女乘客比例的扇形圖
通過創(chuàng)建扇形圖,我們展示了乘客中男性和女性的比例。這是一個簡單而有效的方式來可視化性別分布,并了解男女乘客的比例。
# 運行以下代碼
# sum the instances of males and females
males = (titanic['Sex'] == 'male').sum()
females = (titanic['Sex'] == 'female').sum()
# put them into a list called proportions
proportions = [males, females]
# Create a pie chart
plt.pie(
# using proportions
proportions,
# with the labels being officer names
labels = ['Males', 'Females'],
# with no shadows
shadow = False,
# with colors
colors = ['blue','red'],
# with one slide exploded out
explode = (0.15 , 0),
# with the start angle at 90%
startangle = 90,
# with the percent listed as a fraction
autopct = '%1.1f%%'
)
# View the plot drop above
plt.axis('equal')
# Set labels
plt.title("Sex Proportion")
# View the plot
plt.tight_layout()
plt.show()
步驟6 繪制一個展示船票Fare, 與乘客年齡和性別的散點圖
在這一步,我們創(chuàng)建了一個散點圖,將船票價格 (Fare) 與乘客的年齡和性別進行了比較。這種圖表可以幫助我們觀察票價與年齡和性別之間的關(guān)系。
# 運行以下代碼 # creates the plot using lm = sns.lmplot(x = 'Age', y = 'Fare', data = titanic, hue = 'Sex', fit_reg=False) # set title lm.set(title = 'Fare x Age') # get the axes object and tweak it axes = lm.axes axes[0,0].set_ylim(-5,) axes[0,0].set_xlim(-5,85)
(-5.0, 85.0)

步驟7 有多少人生還?
我們統(tǒng)計了生還乘客的數(shù)量,這是泰坦尼克災(zāi)難中一個重要的統(tǒng)計指標。在這個數(shù)據(jù)集中,有342人幸存下來。
# 運行以下代碼 titanic.Survived.sum()
342
步驟8 繪制一個展示船票價格的直方圖
最后,我們創(chuàng)建了一個直方圖,顯示了不同船票價格的頻率分布。這種圖表可以幫助我們了解船票價格的分布情況。
# 運行以下代碼
# sort the values from the top to the least value and slice the first 5 items
df = titanic.Fare.sort_values(ascending = False)
df
# create bins interval using numpy
binsVal = np.arange(0,600,10)
binsVal
# create the plot
plt.hist(df, bins = binsVal)
# Set the title and labels
plt.xlabel('Fare')
plt.ylabel('Frequency')
plt.title('Fare Payed Histrogram')
# show the plot
plt.show()
總結(jié)
我們介紹了數(shù)據(jù)可視化的基本步驟,并使用了 matplotlib 和 seaborn 庫來創(chuàng)建不同類型的圖表。具體來說,我們使用了扇形圖展示比例、散點圖展示關(guān)系、直方圖展示分布情況等。這些圖表有助于我們更好地理解泰坦尼克災(zāi)難數(shù)據(jù),探索性別比例、票價分布以及生還人數(shù)等方面的信息。
1、Seaborn簡介和基本繪圖函數(shù)
Seaborn 是一個用于數(shù)據(jù)可視化的 Python 庫,它建立在 Matplotlib 的基礎(chǔ)之上,提供了更高級、更美觀和更方便的繪圖功能。以下是 Seaborn 中常用的一些創(chuàng)建圖表的函數(shù):
- sns.scatterplot(): 用于創(chuàng)建散點圖,展示兩個變量之間的關(guān)系。
- sns.lineplot(): 繪制線圖,通常用于顯示時間序列數(shù)據(jù)的趨勢。
- sns.barplot(): 創(chuàng)建條形圖,用于比較不同類別之間的數(shù)值關(guān)系。
- sns.countplot(): 繪制計數(shù)圖,用于顯示每個類別的頻數(shù)或計數(shù)。
- sns.boxplot(): 繪制箱線圖,展示數(shù)據(jù)的分布和異常值。
- sns.violinplot(): 創(chuàng)建小提琴圖,結(jié)合了箱線圖和核密度估計,用于展示數(shù)據(jù)分布。
- sns.heatmap(): 生成熱力圖,通常用于顯示相關(guān)性矩陣或二維數(shù)據(jù)的值分布。
- sns.pairplot(): 創(chuàng)建成對關(guān)系圖,展示數(shù)據(jù)集中多個變量之間的散點圖和直方圖。
- sns.distplot(): 繪制單變量的分布圖,包括直方圖和核密度估計。
- sns.jointplot(): 創(chuàng)建聯(lián)合圖,同時顯示兩個變量的單變量分布和二維關(guān)系。
- sns.lmplot(): 繪制線性回歸模型的散點圖和回歸線。
- sns.catplot(): 用于創(chuàng)建分類圖,可以包括多個子圖,通常用于比較不同組或類別之間的關(guān)系。
- sns.relplot(): 繪制關(guān)系圖,用于可視化兩個或多個變量之間的關(guān)系。
2、前文中所用的Seaborn
以前文所使用的lmplot為例:sns.lmplot() 是 Seaborn 庫中用于創(chuàng)建線性回歸模型圖的函數(shù)。它通常用于可視化兩個變量之間的線性關(guān)系,同時可以添加回歸線和置信區(qū)間。
sns.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, height=5, aspect=1, markers='o', scatter_kws=None, line_kws=None)
參數(shù)說明:
x和y:分別是數(shù)據(jù)框中的列名,用于表示 x 軸和 y 軸上的變量。data:要使用的數(shù)據(jù)框,包含 x 和 y 列。hue:可選參數(shù),用于根據(jù)某一列的不同取值給數(shù)據(jù)點著色,通常用于表示分類變量。col和row:可選參數(shù),用于在多個子圖中繪制不同的圖表,通常用于表示分類變量。palette:可選參數(shù),用于指定顏色調(diào)色板,用于著色數(shù)據(jù)點或線條。height:圖的高度。aspect:圖的縱橫比。markers:可選參數(shù),用于指定數(shù)據(jù)點的標記樣式。scatter_kws和line_kws:可選參數(shù),用于傳遞給散點圖和回歸線的其他參數(shù)。
sns.lmplot() 函數(shù)的作用是繪制散點圖,并根據(jù)數(shù)據(jù)擬合一個線性回歸模型。它通常用于研究兩個連續(xù)變量之間的關(guān)系,以及回歸模型的擬合情況。
import seaborn as sns
import matplotlib.pyplot as plt
# 創(chuàng)建一個示例數(shù)據(jù)框
data = sns.load_dataset("tips")
# 使用lmplot繪制散點圖和回歸線
sns.lmplot(x="total_bill", y="tip", data=data)
# 添加回歸線的置信區(qū)間
sns.lmplot(x="total_bill", y="tip", data=data, ci=None)
# 使用hue參數(shù)根據(jù)分類變量著色
sns.lmplot(x="total_bill", y="tip", data=data, hue="sex")
# 使用col和row參數(shù)創(chuàng)建多個子圖
sns.lmplot(x="total_bill", y="tip", data=data, col="time", row="sex")
# 自定義標記樣式和顏色
sns.lmplot(x="total_bill", y="tip", data=data, markers=["o", "x"], palette="Set1")
plt.show()3、Seaborn的樣式控制
Seaborn 允許你控制圖形的樣式和外觀,以使你的數(shù)據(jù)可視化更加美觀和易于理解。你可以使用 Seaborn 提供的各種樣式設(shè)置和主題來自定義圖形的外觀。
樣式設(shè)置(Style Set): Seaborn 提供了不同的樣式設(shè)置,通過 sns.set_style() 函數(shù)可以進行切換。常用的樣式設(shè)置包括 "whitegrid"、"darkgrid"、"white"、"dark" 和 "ticks"。每種樣式設(shè)置具有不同的背景和網(wǎng)格線樣式,你可以根據(jù)需要選擇合適的樣式。
import seaborn as sns
# 使用不同的樣式設(shè)置
sns.set_style("whitegrid")
sns.set_style("darkgrid")
sns.set_style("white")
sns.set_style("dark")
sns.set_style("ticks")圖形主題(Themes): 通過 sns.set_theme() 函數(shù),你可以選擇不同的圖形主題,包括 "darkgrid"、"whitegrid"、"dark"、"white" 和 "ticks",這與樣式設(shè)置類似。不同的主題將影響整個圖形的外觀。
import seaborn as sns # 使用不同的圖形主題 sns.set_theme(style="darkgrid") sns.set_theme(style="whitegrid") sns.set_theme(style="dark") sns.set_theme(style="white") sns.set_theme(style="ticks")
顏色調(diào)色板(Color Palettes): Seaborn 提供了各種顏色調(diào)色板,用于指定圖形中的顏色。你可以使用 sns.color_palette() 函數(shù)來自定義顏色調(diào)色板,也可以使用已經(jīng)定義好的顏色調(diào)色板,如 "deep"、"pastel"、"dark" 等。
import seaborn as sns
# 使用不同的顏色調(diào)色板
sns.color_palette("deep")
sns.color_palette("pastel")
sns.color_palette("dark")坐標軸刻度(Axis Ticks): 你可以通過 sns.set_context() 函數(shù)來控制坐標軸刻度的大小和字體??蛇x的上下文包括 "paper"、"notebook"、"talk" 和 "poster"。
import seaborn as sns
# 設(shè)置坐標軸刻度的上下文
sns.set_context("paper")
sns.set_context("notebook")
sns.set_context("talk")
sns.set_context("poster")這些控制樣式的方法可以根據(jù)你的需求來自定義 Seaborn 圖形的外觀,使其更符合你的數(shù)據(jù)可視化目標和審美標準。你可以根據(jù)具體情況組合使用這些設(shè)置來創(chuàng)建最適合你的圖形樣式。
以上就是pandas探索你的數(shù)據(jù)實現(xiàn)可視化示例詳解的詳細內(nèi)容,更多關(guān)于pandas數(shù)據(jù)可視化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python scipy.spatial.distance 距離計算函數(shù) ?
本文主要介紹了python scipy.spatial.distance 距離計算函數(shù),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
pycharm創(chuàng)建django項目出現(xiàn)路徑錯誤問題
在PyCharm中創(chuàng)建Django項目時,若使用之前項目的環(huán)境編譯器,且已修改其根目錄,則新建項目路徑可能出錯。解決辦法是在設(shè)置中選擇Project,通過齒輪圖標進入Show?All,選擇編譯器路徑,點擊筆形圖修改Development?configuration的Root?path為/,以確保新項目能正確創(chuàng)建2024-09-09
Pandas計算元素的數(shù)量和頻率的方法(出現(xiàn)的次數(shù))
本文主要介紹了Pandas計算元素的數(shù)量和頻率的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02

