Add the mean in box plots with plotly express?

huangapple go评论59阅读模式
英文:

Add the mean in box plots with plotly express?

问题

I have the following figure:

Add the mean in box plots with plotly express?

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:

Add the mean in box plots with plotly express?

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()

huangapple
  • 本文由 发表于 2023年5月10日 17:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定