gpt4 book ai didi

python - 如何将中位数和 IQR 添加到 seaborn violinplot

转载 作者:行者123 更新时间:2023-12-05 04:37:19 25 4
gpt4 key购买 nike

我想将中位数和智商值添加到 fiddle 图中。但是,我没有找到它的论据。

import seaborn as sns
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x=tips["total_bill"])
ax = sns.violinplot(x="day", y="total_bill", hue="sex",
data=tips, palette="Set2", split=True,
scale="count", inner="quartile")
plt.show()

Violin Plot

例如,我想为所有按性别的图表使用这样的符号:

Violin plot new

如果其他软件包可用,我也可以这样做。但是,我想要 fiddle 图而不是箱线图。

最佳答案

如果您查看 ax 中的线,您会发现它们包含四分位数和中线的坐标(见下文)

所以我们可以只从每一行的第一个数组中取出非零元素,然后从第二个数组的第一个元素中获取 x 和 y,并将 y 作为文本值。

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(rc={'figure.figsize':(20,14)})
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x="day", y="total_bill", hue="sex",
data=tips, palette="Set2", split=True,
scale="count", inner="quartile")

for l in ax.lines:
ax.text(l.get_data()[0][l.get_data()[0].nonzero()][0], l.get_data()[1][0], f'{l.get_data()[1][0]:.0f}',size='large')

enter image description here

for l in ax.lines:
print(l.get_data())

输出

(array([-0.33143126,  0.        ]), array([13.6975, 13.6975]))
(array([-0.37082654, 0. ]), array([16.975, 16.975]))
(array([-0.25003508, 0. ]), array([22.36, 22.36]))
(array([0. , 0.38725949]), array([12.1625, 12.1625]))
(array([0. , 0.39493527]), array([13.785, 13.785]))
(array([0. , 0.25722566]), array([18.675, 18.675]))
(array([0.61440819, 1. ]), array([12.235, 12.235]))
(array([0.62200517, 1. ]), array([17.215, 17.215]))
(array([0.71996702, 1. ]), array([26.0825, 26.0825]))
(array([1. , 1.27676537]), array([11.35, 11.35]))
(array([1. , 1.35376254]), array([15.38, 15.38]))
(array([1. , 1.33610081]), array([16.27, 16.27]))
(array([1.63945286, 2. ]), array([13.905, 13.905]))
(array([1.60991669, 2. ]), array([18.24, 18.24]))
(array([1.75431475, 2. ]), array([24.165, 24.165]))
(array([2. , 2.17418026]), array([14.05, 14.05]))
(array([2. , 2.18356376]), array([18.36, 18.36]))
(array([2. , 2.12950778]), array([25.5625, 25.5625]))
(array([2.62638135, 3. ]), array([15.135, 15.135]))
(array([2.61919886, 3. ]), array([20.725, 20.725]))
(array([2.73148987, 3. ]), array([26.55, 26.55]))
(array([3. , 3.12054879]), array([15.175, 15.175]))
(array([3. , 3.12176923]), array([17.41, 17.41]))
(array([3. , 3.07327664]), array([24.8975, 24.8975]))

关于python - 如何将中位数和 IQR 添加到 seaborn violinplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70714905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com