- Seaborn 教程
- Seaborn - 首頁
- Seaborn - 簡介
- Seaborn - 環境設定
- 匯入資料集和庫
- Seaborn - 圖表美化
- Seaborn - 顏色調色盤
- Seaborn - 直方圖
- Seaborn - 核密度估計
- 視覺化成對關係
- Seaborn - 繪製分類資料
- 觀測值的分佈
- Seaborn - 統計估計
- Seaborn - 繪製寬格式資料
- 多面板分類圖
- Seaborn - 線性關係
- Seaborn - Facet Grid
- Seaborn - Pair Grid
- 函式參考
- Seaborn - 函式參考
- Seaborn 有用資源
- Seaborn - 快速指南
- Seaborn - 有用資源
- Seaborn - 討論
Seaborn.violinplot() 方法
Seaborn.violinplot() 方法允許使用者繪製箱線圖以及核密度估計圖。小提琴圖和箱線圖具有相同的用途。為了便於比較,它顯示了在一個(或多個)分類變數的多個水平上定量資料的分佈。
與箱線圖(其中所有繪圖元素都對應於實際資料點)不同,小提琴圖包含對基礎分佈的核密度估計。
語法
以下是 violinplot() 方法的語法:
seaborn.violinplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)
引數
下面討論 seaborn.violinplot() 方法的一些引數。
| 序號 | 引數和描述 |
|---|---|
| 1 | x,y 這些引數接受變數名作為輸入,以繪製長格式資料。 |
| 2 | data 這是用於繪製圖形的資料框。 |
| 3 | hue 資料框中用於繪製圖形的變數名稱。 |
| 4 | linewidth 此引數接受浮點值,並確定構成繪圖中元素的灰色線條的寬度。 |
| 5 | dodge 此引數接受布林值。如果使用 hue 巢狀,則將 true 傳遞給此引數將分離不同 hue 水平的條帶。如果傳遞 False,則每個級別的點將彼此疊加繪製。 |
| 6 | orient 它接受值“h”或“v”,圖形的方向根據此值確定。 |
| 7 | color 接受 matplotlib 顏色作為輸入,這決定了所有元素的顏色。 |
| 8 | size 此引數確定繪製的圖中標記的半徑。 |
| 9 | palette 此引數用於設定圖的顏色調色盤。 |
| 10 | inner 此可選引數接受以下值:“box”、“quartile”、“point”、“stick”,None。它用於表示小提琴內部的資料點。 |
| 11 | scale 此引數用於縮放每個小提琴的寬度。它接受值 count、width 和 area。 |
| 12 | scale_hue 此引數控制當使用 hue 變數分層小提琴時,縮放是在圖上的所有小提琴中計算,還是在主要分組變數的每個級別內計算 (scale_hue=False)。 |
| 13 | bw 它接受以下值之一:浮點數、“scott”、“silverman”和可選。 用於計算核頻寬的縮放因子,或參考規則的名稱。縮放因子乘以每個箱內資料的標準差將得出實際的核大小。 |
載入 seaborn 庫
在繼續開發繪圖之前,讓我們載入 seaborn 庫和資料集。要載入或匯入 seaborn 庫,可以使用以下程式碼行。
Import seaborn as sns
載入資料集
在本文中,我們將使用 seaborn 庫中內建的泰坦尼克號資料集。使用以下命令載入資料集。
titanic=sns.load_dataset("titanic")
以下命令用於檢視資料集中前 5 行。這使我們能夠了解可以使用哪些變數來繪製圖形。
titanic.head()
以下是上述程式碼段的輸出。
index,survived,pclass,sex,age,sibsp,parch,fare,embarked,class,who,adult_male,deck,embark_town,alive,alone 0,0,3,male,22.0,1,0,7.25,S,Third,man,true,NaN,Southampton,no,false 1,1,1,female,38.0,1,0,71.2833,C,First,woman,false,C,Cherbourg,yes,false 2,1,3,female,26.0,0,0,7.925,S,Third,woman,false,NaN,Southampton,yes,true
現在我們已經載入了資料集,我們將探索一些示例。
示例 1
在這個示例中,我們將瞭解如何使用 seaborn.violinplot() 方法。我們將向方法傳遞 x、y、hue 和 palette 引數,然後我們將觀察繪製的圖。我們在這篇文章中使用內建的泰坦尼克號資料集。以下程式碼行使用小提琴圖方法的 x、y、hue 和 palette 引數。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.violinplot(x="who", y="age", data=titanic,hue="class",palette="muted")
plt.show()
輸出
生成的輸出圖如下:
示例 2
在這個示例中,我們將瞭解 inner 引數的用法。此可選引數接受以下值:“box”、“quartile”、“point”、“stick”,None。它用於表示小提琴內部的資料點。可以使用以下程式碼行來了解 inner 引數的用法。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.violinplot(x="survived", y="age", data=titanic,hue="class",palette="muted",inner="quartile")
plt.show()
輸出
上述程式碼行的輸出如下:
示例 3
violinplot() 方法有很多引數。我們將瞭解如何使用 scale 和 scale_hue 引數。scale 引數用於縮放每個小提琴的寬度。它接受值 count、width 和 area。scale_hue 引數控制當使用 hue 變數分層小提琴時,縮放是在圖上的所有小提琴中計算,還是在主要分組變數的每個級別內計算 (scale_hue=False)。本例中描述了這些引數以及上述引數。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.violinplot(x="survived", y="age", data=titanic,hue="class",palette="muted",inner="stick",scale="count",scale_hue=False)
plt.show()
輸出
上述程式碼片段的輸出如下:
示例 4
我們將在此示例中與所有其他引數一起使用 bw 引數。它接受以下值之一:浮點數、“scott”、“silverman”和可選。用於計算核頻寬的縮放因子,或參考規則的名稱。縮放因子乘以每個箱內資料的標準差將得出實際的核大小。可以使用以下程式碼行來實現。
#bw is used to narrow bandwith to reduce smoothing
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.violinplot(x="survived", y="age", data=titanic,hue="class",palette="muted",inner="quartile",scale="count",bw=.2)
plt.show()
輸出
上述程式碼的輸出顯示如下。