seaborn.violinplot

优质
小牛编辑
130浏览
2023-12-01
seaborn.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)

结合箱型图与核密度估计绘图。

小提琴图的功能与箱型图类似。 它显示了一个(或多个)分类变量多个属性上的定量数据的分布,从而可以比较这些分布。与箱形图不同,其中所有绘图单元都与实际数据点对应,小提琴图描述了基础数据分布的核密度估计。

小提琴图可以是一种单次显示多个数据分布的有效且有吸引力的方式,但请记住,估计过程受样本大小的影响,相对较小样本的小提琴可能看起来非常平滑,这种平滑具有误导性。

输入数据可以通过多种格式传入,包括:

  • 格式为列表,numpy数组或pandas Series对象的数据向量可以直接传递给xyhue参数。
  • 对于长格式的DataFrame,xy,和hue参数会决定如何绘制数据。
  • 对于宽格式的DataFrame,每一列数值列都会被绘制。
  • 一个数组或向量的列表。

在大多数情况下,可以使用numpy或Python对象,但更推荐使用pandas对象,因为与数据关联的列名/行名可以用于标注横轴/纵轴的名称。此外,您可以使用分类类型对变量进行分组以控制绘图元素的顺序。

此函数始终将其中一个变量视为分类,并在相关轴上的序数位置(0,1,... n)处绘制数据,即使数据属于数值类型或日期类型也是如此。

更多信息请参阅 color_palette() 得到一些解释,或者将色调级别映射到matplotlib颜色的字典。

saturation:float,可选

控制用于绘制颜色的原始饱和度的比例。通常大幅填充在轻微不饱和的颜色下看起来更好,如果您希望绘图颜色与输入颜色规格完美匹配可将其设置为1

ax:matplotlib轴,可选

绘图时使用的Axes轴对象,否则使用当前Axes轴对象。

返回值:ax:matplotlib轴

返回Axes对轴象,并在其上绘制绘图。

亦可参见

一个传统的箱型图具有类似的API。当一个变量是分类变量的散点图。可以与其他图表结合使用以展示各自的观测结果。分类散点图的特点是其中数据点互不重叠。可以与其他图表结合使用以展示各自的观测结果。

示例

绘制一个单独的横向小提琴图:

>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.violinplot(x=tips["total_bill"])

http://seaborn.pydata.org/_images/seaborn-violinplot-1.png

根据分类变量分组绘制一个纵向的小提琴图:

>>> ax = sns.violinplot(x="day", y="total_bill", data=tips)

http://seaborn.pydata.org/_images/seaborn-violinplot-2.png

根据2个分类变量嵌套分组绘制一个小提琴图:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
...                     data=tips, palette="muted")

http://seaborn.pydata.org/_images/seaborn-violinplot-3.png

绘制分割的小提琴图以比较不同的色调变量:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="smoker",
...                     data=tips, palette="muted", split=True)

http://seaborn.pydata.org/_images/seaborn-violinplot-4.png

通过显式传入参数指定顺序控制小提琴图的显示顺序:

>>> ax = sns.violinplot(x="time", y="tip", data=tips,
...                     order=["Dinner", "Lunch"])

http://seaborn.pydata.org/_images/seaborn-violinplot-5.png

将小提琴宽度缩放为每个分箱中观察到的数据点数目:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
...                     data=tips, palette="Set2", split=True,
...                     scale="count")

http://seaborn.pydata.org/_images/seaborn-violinplot-6.png

将四分位数绘制为水平线而不是迷你箱型图:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
...                     data=tips, palette="Set2", split=True,
...                     scale="count", inner="quartile")

http://seaborn.pydata.org/_images/seaborn-violinplot-7.png

用小提琴图内部的横线显示每个观察到的数据点:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
...                     data=tips, palette="Set2", split=True,
...                     scale="count", inner="stick")

http://seaborn.pydata.org/_images/seaborn-violinplot-8.png

根据所有分箱的数据点数目对密度进行缩放:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
...                     data=tips, palette="Set2", split=True,
...                     scale="count", inner="stick", scale_hue=False)

http://seaborn.pydata.org/_images/seaborn-violinplot-9.png

使用窄带宽来减少平滑量:

>>> ax = sns.violinplot(x="day", y="total_bill", hue="sex",
...                     data=tips, palette="Set2", split=True,
...                     scale="count", inner="stick",
...                     scale_hue=False, bw=.2)

http://seaborn.pydata.org/_images/seaborn-violinplot-10.png

绘制横向小提琴图:

>>> planets = sns.load_dataset("planets")
>>> ax = sns.violinplot(x="orbital_period", y="method",
...                     data=planets[planets.orbital_period < 1000],
...                     scale="width", palette="Set3")

http://seaborn.pydata.org/_images/seaborn-violinplot-11.png

不要让密度超出数据中的极端数值:

>>> ax = sns.violinplot(x="orbital_period", y="method",
...                     data=planets[planets.orbital_period < 1000],
...                     cut=0, scale="width", palette="Set3")

http://seaborn.pydata.org/_images/seaborn-violinplot-12.png

使用 hue 而不改变小提琴图的位置或宽度:

>>> tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
>>> ax = sns.violinplot(x="day", y="total_bill", hue="weekend",
...                     data=tips, dodge=False)

http://seaborn.pydata.org/_images/seaborn-violinplot-13.png

catplot()violinplot() 以及 FacetGrid 结合起来使用。这允许您通过额外的分类变量进行分组。使用 catplot() 比直接使用 FacetGrid 更为安全,因为它保证了不同切面上变量同步的顺序:

>>> g = sns.catplot(x="sex", y="total_bill",
...                 hue="smoker", col="time",
...                 data=tips, kind="violin", split=True,
...                 height=4, aspect=.7);

http://seaborn.pydata.org/_images/seaborn-violinplot-14.png