目录

Seaborn - 绘制分类数据( Plotting Categorical Data)

在前面的章节中,我们学习了散点图,hexbin图和kde图,用于分析研究中的连续变量。 当研究中的变量是分类时,这些图不适合。

当研究中的一个或两个变量是分类时,我们使用像striplot(),swarmplot()等那样的图。 Seaborn提供了这样做的界面。

分类散点图

在本节中,我们将了解分类散点图。

stripplot()

当研究中的一个变量是分类时,使用stripplot()。 它表示沿任意一个轴排序的数据。

例子 (Example)

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()

输出 (Output)

框

在上图中,我们可以清楚地看到每个物种中petal_length的差异。 但是,上述散点图的主要问题是散点图上的点是重叠的。 我们使用'Jitter'参数来处理这种情况。

抖动会为数据添加一些随机噪声。 此参数将调整沿分类轴的位置。

例子 (Example)

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()

输出 (Output)

点

现在,可以很容易地看到点的分布。

Swarmplot()

另一个可以用作'抖动'替代的选项是函数swarmplot() 。 此功能将散点图的每个点定位在分类轴上,从而避免重叠点 -

例子 (Example)

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()

输出 (Output)

灯罩
↑回到顶部↑
WIKI教程 @2018