📜  在Python中使用 Seaborn 的 Violinplot

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

在Python中使用 Seaborn 的 Violinplot

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

小提琴剧情

小提琴情节通过胡须或箱线图进行类似的活动。因为它显示了一个或多个分类变量的多个定量数据。在多个单元显示多个数据可能是一种有效且有吸引力的方式。 “宽格式”数据框有助于维护可以在图表上绘制的每个数字列。可以使用 NumPy 或Python对象,但最好使用 pandas 对象,因为相关名称将用于注释轴。

返回:此方法返回带有绘图的 Axes 对象。

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

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


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


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


Python3
# Python program to illustrate
# violinplot 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.violinplot(x=tip["total_bill"])


Python3
# Python program to illustrate
# violinplot 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.violinplot(x="tip", y="day", data=tip)


Python3
# Python program to illustrate
# violinplot 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.violinplot(x="day", y="total_bill", hue="time", data=tips)


Python3
# Python program to illustrate
# violinplot 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.violinplot(x ='day', y ='tip', data = tip, linewidth = 4)


Python3
# Python program to illustrate
# violinplot 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.violinplot(x="day", y="total_bill", hue="smoker",
                   data=tips, palette="Set2", dodge=True)


Python3
# Python program to illustrate
# violinplot 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.violinplot(x="time", y="tip", data=tips,
                   order=["Dinner", "Lunch"])


Python3
# Python program to illustrate
# violinplot 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.violinplot(x='day', y='total_bill',
                   data=tips, hue='time', palette='pastel')


Python3
# Python program to illustrate
# violinplot 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.violinplot(x ='day', y ='tip',
                   data = tip, saturation =0.03)


Python3
# Python program to illustrate
# violinplot 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.violinplot(x ='day', y ='tip', data = tip, color = "Yellow")


Python3
# Python program to illustrate
# violinplot 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.violinplot(x="day", y="total_bill", hue="sex",
                    data=tip, palette="Set2", split=True,
                    scale="count")


输出:

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

Python3

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

输出:

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

Python3

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

输出:

在 Seaborn violinplot 中对具有不同属性的变量进行分组:

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

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

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

句法:

seaborn.violinplot(x)

代码:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x=tip["total_bill"])

输出:

2.绘制水平小提琴图:

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

Python3

# Python program to illustrate
# violinplot 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.violinplot(x="tip", y="day", data=tip)

输出:

3.使用色调参数:

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

句法:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x="day", y="total_bill", hue="time", data=tips)

输出:

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

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

句法:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x ='day', y ='tip', data = tip, linewidth = 4)

输出:

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

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

句法:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x="day", y="total_bill", hue="smoker",
                   data=tips, palette="Set2", dodge=True)

输出:

7.通过传递显式命令来控制小提琴顺序:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x="time", y="tip", data=tips,
                   order=["Dinner", "Lunch"])

输出:

8.添加调色板属性:

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

Python3

# Python program to illustrate
# violinplot 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.violinplot(x='day', y='total_bill',
                   data=tips, hue='time', palette='pastel')

输出:

9.添加饱和度参数:

绘制颜色的原始饱和度的比例。大色块通常看起来更好,颜色稍微不饱和,但如果您希望绘图颜色与输入颜色规范完美匹配,请将其设置为 1。

句法:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x ='day', y ='tip',
                   data = tip, saturation =0.03)

输出:

10.添加颜色参数:

它将为渐变调色板的所有元素或种子着色。

句法:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x ='day', y ='tip', data = tip, color = "Yellow")

输出:

11. 根据每个 bin 中的观察次数来缩放小提琴宽度:

Python3

# Python program to illustrate
# violinplot 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.violinplot(x="day", y="total_bill", hue="sex",
                    data=tip, palette="Set2", split=True,
                    scale="count")

输出: