- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,我们的数据集 tips
包含列 day
、total_bill
和 sex
。我想可视化箱线图(x=day
,y=total_bill
,color=sex
)。之后我想计算女性和男性参与者之间每天的测试和 p 值。如果 p 值 < 0.05,我想添加星号。我怎样才能更改下面的代码?
在这个例子中,不同日子之间没有做爱的比较:
from scipy import stats
import plotly.express as px
import plotly.graph_objects as go
tips = px.data.tips()
fig = go.Figure()
for day in ['Thur','Fri','Sat','Sun']:
fig.add_trace(go.Box(
y=tips[tips['day'] == day].total_bill,
name=day,
boxpoints='outliers'
))
def add_pvalue_annotation(days, y_range, symbol=''):
"""
arguments:
days --- a list of two different days e.g. ['Thur','Sat']
y_range --- a list of y_range in the form [y_min, y_max] in paper units
"""
pvalue = stats.ttest_ind(
tips[tips['day']==days[0]].total_bill,
tips[tips['day']==days[1]].total_bill)[1]
# print(pvalue)
if pvalue >= 0.05:
symbol = 'ns'
if pvalue < 0.05:
symbol = '*'
fig.add_shape(type="line",
xref="x", yref="paper",
x0=days[0], y0=y_range[0], x1=days[0], y1=y_range[1],
line=dict(
color="black",
width=2,
)
)
fig.add_shape(type="line",
xref="x", yref="paper",
x0=days[0], y0=y_range[1], x1=days[1], y1=y_range[1],
line=dict(
color="black",
width=2,
)
)
fig.add_shape(type="line",
xref="x", yref="paper",
x0=days[1], y0=y_range[1], x1=days[1], y1=y_range[0],
line=dict(
color="black",
width=2,
)
)
## add text at the correct x, y coordinates
## for bars, there is a direct mapping from the bar number to 0, 1, 2...
bar_xcoord_map = {x: idx for idx, x in enumerate(['Thur','Fri','Sat','Sun'])}
fig.add_annotation(dict(font=dict(color="black",size=14),
x=(bar_xcoord_map[days[0]] + bar_xcoord_map[days[1]])/2,
y=y_range[1]*1.03,
showarrow=False,
text=symbol,
textangle=0,
xref="x",
yref="paper"
))
add_pvalue_annotation(['Thur','Sun'],[1.01,1.02])
add_pvalue_annotation(['Thur','Sat'],[1.05,1.06])
fig.show()
我在这里找到了这个有用的例子:Plotly box p-value significant annotation
最佳答案
当您设置箱线图时,使用 plotly.express 中的 px.box
会很有用,因为您可以传递参数 color="sex"
来创建每天每个性别的两个箱线图。您还需要对 tips
DataFrame 进行排序,以便按顺序绘制星期几。
然后可以修改 add_pvalue_annotation
函数,以便我们计算每天男性和女性之间 t 检验的 p 值(而不是不同日期提示之间的 t 检验一周中的)。您还需要更改注释的起点和终点,以便它们位于同一天的“男性”和“女性”类别之间,而不是在不同的日期之间。
对于 tips
数据集,我在一周中的每一天都对男性和女性进行了 t 检验(例如男性和女性在周四,男性和女性在周五...),并且没有一个 p 值低于 0.05。
但是,为了证明 add_pvalue_annotation
函数会正确放置注释,我将 p 值阈值设置为 0.15,以便周五男性和女性之间的 p 值(p- value = 0.13
) 将在图表上进行注释。
from scipy import stats
import plotly.express as px
import plotly.graph_objects as go
from pandas.api.types import CategoricalDtype
tips = px.data.tips()
cat_order = ['Thur', 'Fri', 'Sat', 'Sun']
cat_weekdays = CategoricalDtype(cat_order, ordered=True)
tips['day'] = tips['day'].astype(cat_weekdays)
tips.sort_values(by='day', inplace=True)
fig = px.box(tips, x="day", y="total_bill", color="sex")
def add_pvalue_annotation(day, y_range, symbol='', pvalue_th=0.05):
"""
arguments:
days --- the day for which you want to calculate the p-value on a t-test between Men and Women (e.g. 'Thur')
x_coordinate --- the x-coordinate
y_range --- a list of y_range in the form [y_min, y_max] in paper units
"""
pvalue = stats.ttest_ind(
tips[(tips['day']==day) & (tips['sex'] == 'Male')].total_bill,
tips[(tips['day']==day) & (tips['sex'] == 'Female')].total_bill
)[1]
# print(f"pvalue between men and women on {day}: {pvalue}")
# if pvalue >= pvalue_th:
# symbol = 'ns'
if pvalue < pvalue_th:
## for bars, there is a direct mapping from the bar number to 0, 1, 2...
bar_xcoord_map = {x: idx for idx, x in enumerate(cat_order)}
x_coordinate = bar_xcoord_map[day]
x_start, x_end = x_coordinate - 0.2, x_coordinate + 0.2
symbol = '*'
fig.add_shape(type="line",
xref="x", yref="paper",
x0=x_start, y0=y_range[0], x1=x_start, y1=y_range[1],
line=dict(
color="black",
width=2,
)
)
fig.add_shape(type="line",
xref="x", yref="paper",
x0=x_start, y0=y_range[1], x1=x_end, y1=y_range[1],
line=dict(
color="black",
width=2,
)
)
fig.add_shape(type="line",
xref="x", yref="paper",
x0=x_end, y0=y_range[1], x1=x_end, y1=y_range[0],
line=dict(
color="black",
width=2,
)
)
## add text at the correct x, y coordinates
fig.add_annotation(dict(font=dict(color="black",size=14),
x=x_coordinate,
y=y_range[1]*1.03,
showarrow=False,
text=symbol,
textangle=0,
xref="x",
yref="paper"
))
for day in cat_order:
add_pvalue_annotation(day, [1.01,1.02], pvalue_th=0.15)
fig.show()
关于python - 我可以计算 p 值并使用 plotly 添加星号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70757929/
关于strcat函数。 while (*p) p++; 和 while (*++p) ; 两者都有效,但是 while (*p++) ; 不起作用。我认为 first 和 th
" in HTML?(HTML中的““是什么
?)
下面例子中的第一行代码是什么。我看到一个YouTuber在写下面的代码,它显示了一个设计在csswar Challenges中。我也尝试了一下,它很管用。但我以前从未在任何HTML教程上看到过它,我在
vs.
是不间断空格,表示没有换行的空白处。 如果我用 我在两个段落之间有一个空格(更大的间隔)。如果我使用 我在两个段落之间只有一个新行(没有中断)。为什么? 最佳答案 在 HTML 中
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 9
我对编程还很陌生,只是想知道为什么这段代码: for ( ; *p; ++p) *p = tolower(*p); 当 p 指向一个字符串时,可以降低 c 中字符串的大小写吗? 最佳答案 一般来说,这
代码 int n = 25; int *p = &n; printf("%x\n %d\n %x\n", p, p[0], p[1]); 返回: \ 当然我永远不会这样做,但在 K&R 中声明
所以,我想创建一个简单的程序,返回有关连续素数的计算结果。首先,我创建一个包含所有这些素数的列表,然后尝试计算结果,但这给了我一个超出范围的索引。有人可以帮助我吗?我的程序: primes = []
这个问题在这里已经有了答案: With arrays, why is it the case that a[5] == 5[a]? (20 个答案) 关闭 9 年前。 我想知道 C/C++ 中以下四
我仍在努力理解 *p、&p 和 p 之间的区别。根据我的理解,* 可以被认为是“指向的值”,而 & 可以被认为是“地址”。换句话说,* 保存值,而 & 保存地址。如果这是真的,那么 *p 和 p 之间
你是吗? [xxxrecipientFirstNamexxx]
和你是吗? {recipientFirstName}
需要更换 你是吗? [xxxrecipientFirstNamexxx] 和 你是吗? {recipientFirstName} 。我尝试使用边界匹配器。但结果并不符合预期。我尝试使用下面的代码 "A
我想按 IsTop 属性升序排序对象,然后按 JobId 属性降序排序: query = query.OrderBy(p => p.IsTop).ThenOrderByDescending(p =
在我尝试使用 Apache POI 进行转换的 Excel 文件中,我有一个单元格的数值为 -3.97819466831428,自定义格式为“0.0 p.p.;(0.0 p.p.)”。因此,在 Exc
我想创建一个扩展方法,允许我调用 ToSerializableDictionary(p => p.ID)而不是 .ToDictionary(p => p.ID)在以下 LINQ 上下文中。虽然我不确定
在下面的 HTML 代码上运行此 jQuery 代码会返回不同的结果,我认为它们应该返回相同的值。 jQuery 代码: var counter = 0; $("p").each(function()
在下面的代码片段中,符号 *p 等同于 p[0],*(p + 1) 等同于p[1],依此类推。 int* p = new int[3] { 1, 2, 3}; cout << *p << ' ' <<
这个问题在这里已经有了答案: What will happen when I call a member function on a NULL object pointer? [duplicate]
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Undefined Behavior and Sequence Points 按照标准中的定义,E1 +=
" in HTML?(在HTML中“
以下示例中的第一行代码是什么。我看到一个youtube用户写下面的代码,它显示在cssbattle挑战的设计。我也试过,它的作品。但我从来没有见过它在任何HTML教程之前,我在谷歌上搜索它,但它只显示
每当我收到来自 MS outlook 的电子邮件时,我都会收到此标记 & nbsp ; (没有空格)哪个显示为?在 <>. 当我将其更改为 ISO-8859-1 时,浏览器页面字符集编码为 UTF-8
p1
TESTp2
代码: from bs4 import BeautifulSoup soup = BeautifulSoup('p1TESTp2') print soup.div() 结果: [p1, p2] 为什么
我是一名优秀的程序员,十分优秀!