- Seaborn 教程
- Seaborn - 首頁
- Seaborn - 簡介
- Seaborn - 環境設定
- 匯入資料集和庫
- Seaborn - 圖形美觀
- Seaborn - 調色盤
- Seaborn - 直方圖
- Seaborn - 核密度估計
- 視覺化成對關係
- Seaborn - 繪製分類資料
- 觀測值的分佈
- Seaborn - 統計估計
- Seaborn - 繪製寬格式資料
- 多面板分類圖
- Seaborn - 線性關係
- Seaborn - Facet Grid
- Seaborn - Pair Grid
- 函式參考
- Seaborn - 函式參考
- Seaborn 有用資源
- Seaborn - 快速指南
- Seaborn - 有用資源
- Seaborn - 討論
Seaborn.stripplot() 方法
seaborn.stripplot() 方法用於繪製散點圖,其中作為引數傳遞的變數之一是分類變數。此圖可以單獨繪製,但通常也用於補充其他圖,例如箱線圖,其中除了描繪所有觀測值之外,還需要顯示一些潛在的分佈。
此方法採用不同的格式作為輸入,例如陣列、長格式資料、寬格式資料或向量列表。
語法
以下是 seaborn.stripplot() 方法的語法
seaborn.stripplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs
引數
此方法的一些引數如下所示:
| 序號 | 名稱和描述 |
|---|---|
| 1 | x,y 這些引數將變數名稱作為輸入,以繪製長格式資料。 |
| 2 | data 這是用於繪製圖形的資料框。 |
| 3 | hue 資料框中需要用於繪製圖形的變數名稱。 |
| 4 | jitter 此引數採用浮點值,並根據此值對分類變數的資料進行間隔。它僅適用於分類資料。 |
| 5 | linewidth 此引數採用浮點值,並確定構成圖中元素的灰色線條的寬度。 |
| 6 | dodge 此引數採用布林值。如果我們使用色調巢狀,則將 true 傳遞給此引數將分離不同色調級別的條帶。如果傳遞 False,則每個級別的點將彼此疊加繪製。 |
| 7 | orient 它採用“h”或“v”值,並根據此確定圖形的方向。 |
| 8 | color matplotlib 顏色作為輸入,這決定了所有元素的顏色。 |
在繼續開發繪圖之前,讓我們載入 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
我們將瞭解一個簡單的快捷方式,可用於繪製 stripplot()。與其將資料和 x 作為單獨的引數傳遞,不如將它們組合並作為單個引數在 x 引數中傳遞。可以使用以下程式碼行來實現。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.stripplot(x=titanic["age"])
plt.show()
輸出
上述程式碼行產生的輸出如下所示:
示例 2
現在,我們將連同 x、y 引數一起傳遞 hue 引數,並檢視獲得的圖表的更改。由於我們使用的是泰坦尼克號資料集,因此我們將 alive 變數傳遞給 hue,並將 age 和 sex 分別傳遞給 x 和 dy 引數。可以使用以下程式碼行生成所需的繪圖。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.stripplot(x="age", y="who", hue="alive",data=titanic)
plt.show()
輸出
生成的繪圖如下所示。
示例 3
seaborn.stripplot() 方法中有很多引數,我們將瞭解另一個名為 jitter 的引數的工作原理。jitter 引數用於對分類變數的資料進行間隔,它僅適用於分類型別的資料。它採用浮點值作為輸入。
以下程式碼片段描述了它的用法,生成的繪圖也可以在下面檢視。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
#jitter spaces out categorical data and works only on this type of data.
sns.stripplot(x="age", y="who", data=titanic,jitter=0.5)
plt.show()
輸出
示例 4
seaborn.stripplot() 方法中有很多引數,我們將瞭解另一個名為 jitter 和 linewidth 的引數的工作原理。jitter 引數用於對分類變數的資料進行間隔,它僅適用於分類型別的資料。它採用浮點值作為輸入。linewidth 引數採用浮點值,並確定構成圖中元素的灰色線條的寬度。
以下程式碼片段描述了它的用法,生成的繪圖也可以在下面檢視。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.stripplot(x="age", y="who", hue="alive",data=titanic,jitter=0.5,linewidth=1)
plt.show()
輸出
生成的輸出圖如下所示。
示例 5
在這個示例中,我們將瞭解如何將 dodge 引數與上面使用的引數一起使用。此引數採用布林值。如果我們使用色調巢狀,則將 true 傳遞給此引數將分離不同色調級別的條帶。如果傳遞 False,則每個級別的點將彼此疊加繪製。以下程式碼行可用於使用 dodge 引數繪製圖形。
import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
sns.stripplot(y="fare", x="who", hue="alive",data=titanic,linewidth=1,dodge=True)
plt.show()
輸出
獲得的輸出如下所示: