python 正值的小提琴图

Violin plot for positive values with python

我发现小提琴图非常有用,我使用 python 库 'seaborn'。 然而,当应用于正值时,它们几乎总是在下端显示负值。我发现这确实具有误导性,尤其是在处理现实生活中的数据集时。

在seaborn的官方文档中https://seaborn.pydata.org/generated/seaborn.violinplot.html 可以看到带有 "total_bill" 和 "tip" 的示例,其中 not 可能为负数。 然而,小提琴图显示负值。例如,

import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x="day", y="total_bill", hue="smoker",data=tips, palette="muted", split=True)

我明白,那些负值来自高斯内核。因此,我的问题是:有什么办法可以解决这个问题吗? python 中的另一个图书馆?可以指定不同的内核吗?

您可以使用关键字 cut=0 将绘图限制在数据范围内。如果数据没有负值,这会将小提琴的末尾砍成零。使用与您相同的示例,尝试:

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