📜  在Python中使用 Seaborn 的 Swarmplot

📅  最后修改于: 2022-05-13 01:54:42.352000             🧑  作者: Mango

在Python中使用 Seaborn 的 Swarmplot

Seaborn 是一个惊人的Python统计图形绘图可视化库。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在 matplotlib 库之上,并紧密集成到 pandas 的数据结构中。

群体图

Seaborn swarmplot 可能类似于 stripplot,只是调整了点,因此它不会相互重叠,因为它有助于更好地表示值的分布。群图可以自己绘制,但它也是框的一个很好的补充,因为关联的名称将用于注释轴。这种情节有时被称为“蜂群”。

示例 1:使用 swarmplot() 对“fmri”数据集进行基本可视化

Python3
import seaborn
 
 
seaborn.set(style='whitegrid')
fmri = seaborn.load_dataset("fmri")
 
seaborn.swarmplot(x="timepoint",
                  y="signal",
                  data=fmri)


Python3
import seaborn
 
 
seaborn.set(style='whitegrid')
fmri = seaborn.load_dataset("fmri")
 
seaborn.swarmplot(x="timepoint",
                  y="signal",
                  hue="region",
                  data=fmri)


Python3
import seaborn
 
 
seaborn.set(style='whitegrid')
tip = seaborn.load_dataset('tips')
 
seaborn.swarmplot(x='day', y='tip', data=tip)


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x=tips["total_bill"])


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="total_bill", y="day", data=tips)


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", hue="time", data=tips)


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", data=tips,
                   linewidth=2)


Python3
seaborn.swarmplot(y="total_bill", x="day", data=tips,
                   linewidth=2,edgecolor='green')


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", hue="smoker",
                   data=tips, palette="Set2", dodge=True)


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", hue="smoker",
                   data=tips, palette="Set2", size=20, marker="D",
                   edgecolor="gray", alpha=.25)


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="time", y="tip", data=tips,
                   order=["Dinner", "Lunch"])


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
 
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x='day', y='total_bill', data=tips,
                  hue='smoker', size=10)


Python3
# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
 
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x='day', y='total_bill', data=tips,
                  hue='time', palette='pastel')


输出:

示例 2:根据类别对数据点进行分组,此处为区域和事件。

Python3

import seaborn
 
 
seaborn.set(style='whitegrid')
fmri = seaborn.load_dataset("fmri")
 
seaborn.swarmplot(x="timepoint",
                  y="signal",
                  hue="region",
                  data=fmri)

输出:

示例 3:使用 swarmplot() 对“tips”数据集进行基本可视化

Python3

import seaborn
 
 
seaborn.set(style='whitegrid')
tip = seaborn.load_dataset('tips')
 
seaborn.swarmplot(x='day', y='tip', data=tip)

输出:

Seaborn Swarmplot中具有不同属性的变量分组

1. 仅使用一个轴绘制单个水平群图:

如果我们只使用一个数据变量而不是两个数据变量,那么这意味着轴将这些数据变量中的每一个都表示为一个轴。

X表示x轴,y表示y轴。

句法:

seaborn.swarmplot(x)

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x=tips["total_bill"])

输出:

2.画水平群:

在上面的例子中,我们看到了如何绘制单个水平群图,在这里可以执行多个水平群图,并与另一个轴交换数据变量。

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="total_bill", y="day", data=tips)

输出:

3.使用色调参数:

虽然点是在二维中绘制的,但可以通过根据第三个变量对点着色来将另一个维度添加到图中。

句法:

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", hue="time", data=tips)

输出:

4. 使用线宽在数据点周围绘制轮廓:

构成绘图元素的灰线的宽度。每当我们增加线宽时,点也会自动增加。

句法:

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", data=tips,
                   linewidth=2)

输出:

我们可以用edgecolor 改变颜色:

Python3

seaborn.swarmplot(y="total_bill", x="day", data=tips,
                   linewidth=2,edgecolor='green')

输出:

5. 在主分类轴上的不同位置绘制色调变量的每个级别:

使用色调嵌套时,设置 dodge 应为 True 将沿分类轴分隔不同色调级别的点。而Palette用于不同层次的hue变量。

句法:

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", hue="smoker",
                   data=tips, palette="Set2", dodge=True)

输出:

6. 用marker和alpha参数绘制大点和不同的美学:

我们将使用 alpha 来管理数据点的透明度,并使用 marker for marker 来自定义数据点。

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="day", y="total_bill", hue="smoker",
                   data=tips, palette="Set2", size=20, marker="D",
                   edgecolor="gray", alpha=.25)

输出:

7. 通过传递显式命令来控制群体顺序:

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x="time", y="tip", data=tips,
                   order=["Dinner", "Lunch"])

输出:

8.添加尺寸属性。

使用大小我们可以生成点,我们可以生成不同大小的点。

句法:

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
 
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x='day', y='total_bill', data=tips,
                  hue='smoker', size=10)

输出:

9.添加调色板属性:

使用调色板,我们可以生成具有不同颜色的点。在下面的示例中,我们可以看到调色板可以负责生成具有不同颜色图值的 swramplot。

Python3

# Python program to illustrate
# swarmplot using inbuilt data-set
# given in seaborn
 
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tips = seaborn.load_dataset("tips")
 
seaborn.swarmplot(x='day', y='total_bill', data=tips,
                  hue='time', palette='pastel')

输出: