Seaborn - 繪製分類資料



在我們之前的章節中,我們學習了散點圖、六邊形圖和kde圖,這些圖用於分析所研究的連續變數。當所研究的變數是分類變數時,這些圖不適用。

當一個或兩個研究變數是分類變數時,我們使用stripplot()、swarmplot()等圖。Seaborn 提供了這樣的介面。

分類散點圖

在本節中,我們將學習分類散點圖。

stripplot()

當所研究的變數之一是分類變數時,使用stripplot()。它以排序的方式沿著任何一個軸表示資料。

示例

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('iris')
sb.stripplot(x = "species", y = "petal_length", data = df)
plt.show()

輸出

Box

在上圖中,我們可以清楚地看到每個物種的petal_length差異。但是,上述散點圖的主要問題是散點圖上的點重疊了。我們使用“Jitter”引數來處理這種情況。

Jitter 為資料新增一些隨機噪聲。此引數將調整沿分類軸的位置。

示例

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('iris')
sb.stripplot(x = "species", y = "petal_length", data = df, jitter = Ture)
plt.show()

輸出

Dots

現在,可以很容易地看到點的分佈。

Swarmplot()

另一個可以作為“Jitter”替代方案的選項是函式swarmplot()。此函式將每個散點圖點定位在分類軸上,從而避免點重疊。

示例

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset('iris')
sb.swarmplot(x = "species", y = "petal_length", data = df)
plt.show()

輸出

Shades
廣告
© . All rights reserved.