英文:
Add the mean in box plots with plotly express?
问题
I have the following figure:
and I would like to add the mean.
Is it possible with Plotly Express, without using Graph Objects go.Box()
?
For information, here is my code:
import plotly.express as px
fig = px.box(device_field_strength_impact_df,
y='value',
x='device_field_strength',
color='variable',
# boxmean=True,
facet_col='group',
)
fig.show()
and the pandas DataFrame:
method group patient device_field_strength variable value
expert1 experts 62 device_field_strength_1.5 F1_score 0.857
expert1 experts 66 device_field_strength_3 F1_score 0.909
... ... ... ... ... ... ...
Note: I made the same figure with Graph Objects but I don't know how to handle the legend and x labels.
英文:
I have the following figure:
and I would like to add the mean.
Is it possible with Plotly Express, without using Graph Objects go.Box()
?
For information, here is my code:
import plotly.express as px
fig = px.box(device_field_strength_impact_df,
y='value',
x='device_field_strength',
color='variable',
# boxmean=True,
facet_col='group',
)
fig.show()
and the pandas DataFrame:
method group patient device_field_strength variable value
expert1 experts 62 device_field_strength_1.5 F1_score 0.857
expert1 experts 66 device_field_strength_3 F1_score 0.909
... ... ... ... ... ... ...
Note: I made the same figure with Graph Objects but I don't know how to handle the legend and x labels.
答案1
得分: 1
你可以创建空图,然后使用fig.update_traces
设置箱线图的参数,包括均值:
import plotly.express as px
fig = px.box()
fig.update_traces(
# 箱体
q1=[1, 2, 3],
median=[4, 5, 6],
q3=[7, 8, 9],
# 离群值边界
lowerfence=[-1, 0, 1],
upperfence=[10, 11, 12],
# 均值和标准差
mean=[2.2, 2.8, 4.2],
sd=[0.2, 0.4, 0.6],
notchspan=[0.2, 0.4, 0.6],
boxpoints=False
)
fig.show()
英文:
You can create empty plot and then set parameters for boxplot using fig.update_traces
, including mean:
import plotly.express as px
fig = px.box()
fig.update_traces(
# box
q1=[1, 2, 3],
median=[4, 5, 6],
q3=[7, 8, 9],
# fences
lowerfence=[-1, 0, 1],
upperfence=[10, 11, 12],
# mean and std
mean=[2.2, 2.8, 4.2],
sd=[0.2, 0.4, 0.6],
notchspan=[0.2, 0.4, 0.6],
boxpoints=False
)
fig.show()
Not the most convenient method (you have to calculate values separately), but looks like it works.
答案2
得分: 1
Sure, here is the translated content:
正如 @r-beginners 在他的评论中回答的那样:
> 在 px.box()
后面添加 fig.update_traces(boxmean=True)
可以添加平均值。
确实,以下代码完美运行:
import plotly.express as px
fig = px.box(device_field_strength_impact_df,
y='value',
x='device_field_strength',
color='variable',
facet_col='group',
)
fig.update_traces(boxmean=True)
fig.show()
如果您需要进一步帮助,请告诉我。
英文:
As @r-beginners answered in his comment:
> You can add the averages by adding fig.update_traces(boxmean=True)
after the px.box()
.
Indeed, the following code works perfectly:
import plotly.express as px
fig = px.box(device_field_strength_impact_df,
y='value',
x='device_field_strength',
color='variable',
facet_col='group',
)
fig.update_traces(boxmean=True)
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论