繪制簡單圖,如直方圖耕赘,計數(shù)圖骄蝇,散點圖等。
數(shù)據(jù)集:Titanic號
數(shù)據(jù)集:Titanic號訓(xùn)練集的乘客信息 下載地址:https://itbooks.pipipan.com/fs/18113597-326184444
導(dǎo)入數(shù)據(jù)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
titanic_data = pd.read_csv(r"D:\Datasets\train.csv")
titanic_data.head()
image.png
該數(shù)據(jù)集包含1912年Titanic號的乘客信息操骡,包括姓名九火,年齡,乘客等級册招,是否幸存等岔激。
參考資料
- 討論qq群630011153 144081101 567351477
- 本文最新版本地址
- 本文涉及的python測試開發(fā)庫 謝謝點贊!
- 本文相關(guān)海量書籍下載
- python工具書籍下載-持續(xù)更新
- python GUI工具書籍下載-持續(xù)更新
直方圖
titanic_data['Age'].hist()
使用Pandas數(shù)據(jù)框繪制年齡列的直方圖是多么容易是掰。
image.png
可以將Matplotlib的參數(shù)傳遞給hist()方法虑鼎,因為Pandas在使用了Matplotlib庫。
titanic_data['Age'].hist(bins=20)
image.png
通過導(dǎo)入Seaborn庫設(shè)置set_style屬性值來改進(jìn)圖的樣式键痛。 例如炫彩,讓我們將網(wǎng)格的樣式設(shè)置為深灰色。
import seaborn as sns
sns.set_style('darkgrid')
titanic_data['Age'].plot(kind='hist', bins=20)
數(shù)據(jù)幀有兩種方法繪制圖形散休。 一種方法是給傳遞plot函數(shù)傳遞kind參數(shù):
titanic_data['Age'].plot(kind='hist', bins=20)
另一種方法是使用plot函數(shù)直接調(diào)用繪圖的方法媒楼,參見前面的例子。
折線圖
要使用Pandas數(shù)據(jù)幀繪制折線圖戚丸,您必須使用plot函數(shù)調(diào)用line()方法并傳遞x和y軸的值划址,如下所示:
titanic_data.plot.line(x='Age', y='Fare', figsize=(8,6))
x軸包含乘客的年齡扔嵌,而y軸包含乘客支付的票價。 figsize屬性來改變繪圖的大小夺颤,特別注意這個單位是英尺痢缎。
image.png
散點圖
titanic_data.plot.scatter(x='Age', y='Fare', figsize=(8,6))
image.png
箱體圖
titanic_data.plot.box(figsize=(10,8))
image.png
六角形圖
六邊形圖繪制了在x和y軸上交叉數(shù)據(jù)點的六邊形。 點越多世澜,六邊形越暗独旷。
titanic_data.plot.hexbin(x='Age', y='Fare', gridsize=30, figsize=(8,6))
image.png
密度圖
titanic_data.plot.hexbin(x='Age', y='Fare', gridsize=30, figsize=(8,6))
image.png