How to make Boxplot

Hello,

I am new with dash but I use plotly for several month.
I don’t really understand the structure of dcc.Graph to get the same results as I can get with plotly.
Especially for the box plot. Is there any tutotial somewhere?

Here is my current problem : I want to have two boxplot on the same graph :
Thx for your help.
dcc.Graph(
id=‘Boxplot’,
figure={
‘data’: [{
‘trace0’ : df[‘col1’],
# ‘trace1’ : df[‘col2’], # does not work if I add this line
’type’: ‘box’
}],
‘layout’: {
‘height’: 400,
#‘margin’: {‘l’: 10, ‘b’: 20, ‘t’: 0, ‘r’: 0}
}
}
),

1 Like

The examples in the box plot documentation https://plot.ly/python/box-plots/ can be translated to Dash as:

import plotly.graph_objs as go

import numpy as np

y0 = np.random.randn(50)-1
y1 = np.random.randn(50)+1

trace0 = go.Box(
    y=y0
)
trace1 = go.Box(
    y=y1
)
data = [trace0, trace1]

dcc.Graph(figure={'data': data}, id='box-plot-1')
import plotly.graph_objs as go

trace0 = go.Box(
    y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, 
       8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],
    name = "All Points",
    jitter = 0.3,
    pointpos = -1.8,
    boxpoints = 'all',
    marker = dict(
        color = 'rgb(7,40,89)'),
    line = dict(
        color = 'rgb(7,40,89)')
)

trace1 = go.Box(
    y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, 
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],
    name = "Only Whiskers",
    boxpoints = False,
    marker = dict(
        color = 'rgb(9,56,125)'),
    line = dict(
        color = 'rgb(9,56,125)')
)

trace2 = go.Box(
    y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, 
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],
    name = "Suspected Outliers",
    boxpoints = 'suspectedoutliers',
    marker = dict(
        color = 'rgb(8,81,156)',
        outliercolor = 'rgba(219, 64, 82, 0.6)',
        line = dict(
            outliercolor = 'rgba(219, 64, 82, 0.6)',
            outlierwidth = 2)),
    line = dict(
        color = 'rgb(8,81,156)')
)

trace3 = go.Box(
    y = [0.75, 5.25, 5.5, 6, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, 
        8.15, 8.65, 8.93, 9.2, 9.5, 10, 10.25, 11.5, 12, 16, 20.90, 22.3, 23.25],
    name = "Whiskers and Outliers",
    boxpoints = 'outliers',
    marker = dict(
        color = 'rgb(107,174,214)'),
    line = dict(
        color = 'rgb(107,174,214)')
)

data = [trace0,trace1,trace2,trace3]

layout = go.Layout(
    title = "Box Plot Styling Outliers"
)

fig = go.Figure(data=data,layout=layout)

dcc.Graph(figure=fig, id='box-plot-2')

Thanks for the answer !

Hi,

I have been able to make a box plot for a dataframe data3 using plotly.express

import plotly.express as px 

# Box plots
fig= px.box(data3, x="variable", y="value",points="all",color="study_hospital")
fig.show()

How do I translate this code into Dash code? The given example works on my side. But it plots all the data.
What I trying to create is a databoard with the legend from the column ‘study_hospital’.

Any idea?

dcc.Graph(figure=fig)

1 Like