gpt4 book ai didi

python - 如何插入统计注释(星号或 p 值)

转载 作者:行者123 更新时间:2023-12-02 23:53:35 25 4
gpt4 key购买 nike

这似乎是一个微不足道的问题,但我已经搜索了一段时间,似乎找不到答案。它似乎也应该成为这些软件包的标准部分。有谁知道是否有一种标准方法可以在seaborn中的分布图之间包含统计注释?

例如,在两个方框图或群图之间?

Example: the yellow distribution is significantly different than the others (by wilcoxon - how can i display that visually?

最佳答案

大括号/括号可以直接用 matplotlib.pyplot.plot 绘制或matplotlib.axes.Axes.plot ,并且可以使用 matplotlib.pyplot.text 添加注释或matplotlib.axes.Axes.text .

seaborn 分类图的索引为 0,而默认情况下,matplotlibpandas 的箱线图从 range( 1, N+1),可以通过positions参数进行调整。

seabornmatplotlib 的高级 API,pandas.DataFrame.plot 使用 matplotlib 作为默认后端。

导入和 DataFrame

import seaborn as sns
import matplotlib.pyplot as plt

# dataframe in long form for seaborn
tips = sns.load_dataset("tips")

# dataframe in wide form for plotting with pandas.DataFrame.plot
df = tips.pivot(columns='day', values='total_bill')

# data as a list of lists for plotting directly with matplotlib (no nan values allowed)
data = [df[c].dropna().tolist() for c in df.columns]

seaborn

sns.boxplot(x="day", y="total_bill", data=tips, palette="PRGn")

# statistical annotation
x1, x2 = 2, 3 # columns 'Sat' and 'Sun' (first column: 0, see plt.xticks())
y, h, col = tips['total_bill'].max() + 2, 2, 'k'

plt.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
plt.text((x1+x2)*.5, y+h, "ns", ha='center', va='bottom', color=col)

plt.show()

box plot annotated

pandas.DataFrame.plot

ax = df.plot(kind='box', positions=range(len(df.columns)))

x1, x2 = 2, 3
y, h, col = df.max().max() + 2, 2, 'k'

ax.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
ax.text((x1+x2)*.5, y+h, "ns", ha='center', va='bottom', color=col)

enter image description here

matplotlib

plt.boxplot(data, positions=range(len(data)))

x1, x2 = 2, 3

y, h, col = max(map(max, data)) + 2, 2, 'k'

plt.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
plt.text((x1+x2)*.5, y+h, "ns", ha='center', va='bottom', color=col)

enter image description here

<小时/>

tips.head()

   total_bill   tip     sex smoker  day    time  size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

df.head()

day  Thur  Fri  Sat    Sun
0 NaN NaN NaN 16.99
1 NaN NaN NaN 10.34
2 NaN NaN NaN 21.01
3 NaN NaN NaN 23.68
4 NaN NaN NaN 24.59

数据

[[27.2, 22.76, 17.29, ..., 20.53, 16.47, 18.78],
[28.97, 22.49, 5.75, ..., 13.42, 16.27, 10.09],
[20.65, 17.92, 20.29, ..., 29.03, 27.18, 22.67, 17.82],
[16.99, 10.34, 21.01, ..., 18.15, 23.1, 15.69]]

关于python - 如何插入统计注释(星号或 p 值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36578458/

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