Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add the mean in box plots with plotly express?

I have the following figure:

Box plot from 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.

like image 838
arthur.sw Avatar asked Oct 16 '25 10:10

arthur.sw


1 Answers

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

like image 104
arthur.sw Avatar answered Oct 17 '25 22:10

arthur.sw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!