Hereβs a code that accomplishes fairly well what I want; may there is better solution?
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv')
days=['day1 and day 2', 'day 3 and day 4']
# Group and calculate the mean and sem
mean = df.groupby('day').mean()
sem = df.groupby('day').sem()
# Bar graphs and error bars for top stack only
fig = go.Figure(data=[
go.Bar(name='Thursday and Saturday', x=days, y=[mean_thur, mean_sat], marker_color='#E45746', opacity=0.8),
go.Bar(name='Friday and Sunday', x=days, y=[mean_fri, mean_sun], marker_color='#72B7B2', opacity=0.8,
error_y=dict(
type='data', # value of error bar given in data coordinates
array=[sem_fri, sem_sun], color='rgba(0,0,0,1)', thickness=2, width=10,
visible=True)
)
])
# Error bars for bottom stack
fig.add_trace(go.Scatter(
x=['day1 and day 2'], y=[mean_thur],
mode='markers',
name='error_bars_thursday',
error_y=dict(
type='constant',
value=sem_thur,
color='rgba(0,0,0,1)',
thickness=1.8,
width=10
),
marker=dict(color='rgba(0,0,0,1)', size=10, opacity=0),
showlegend=False
))
fig.add_trace(go.Scatter(
x=['day 3 and day 4'], y=[mean_sat],
mode='markers',
name='error_bars_saturday',
error_y=dict(
type='constant',
value=sem_sat,
color='rgba(0,0,0,1)',
thickness=1.8,
width=10,
),
marker=dict(color='rgba(0,0,0,1)', size=10, opacity=0),
showlegend=False
))
# Add n numbers
fig.add_trace(go.Scatter(
x=['day1 and day 2', 'day 3 and day 4'],
y=[30, 36],
mode="text",
name="n_numbers",
text=['n=20', 'n=50'],
textposition="top center",
showlegend=False
))
# Add brackets for p-values
# Bottom bars
fig.add_trace(go.Scatter(x=['day1 and day 2', 'day1 and day 2', 'day 3 and day 4', 'day 3 and day 4'],
y=[20, 25, 25, 22],
fill=None, mode="lines", line=dict(color='rgba(0,0,0,1)',width=2),
showlegend=False
)
)
# Top bars
fig.add_trace(go.Scatter(x=['day1 and day 2', 'day1 and day 2', 'day 3 and day 4', 'day 3 and day 4'],
y=[40, 47, 47, 45],
fill=None, mode="lines", line=dict(color='rgba(0,0,0,1)',width=2),
showlegend=False
)
)
# Add p-values
fig.add_annotation(text="p=0.00156",
name="p-value",
xref="paper", yref="paper",
x=0.5, y=0.57, showarrow=False,
font=dict(size=12, color="black")
)
fig.add_annotation(text="***",
name="p-value",
xref="paper", yref="paper",
x=0.5, y=1.1, showarrow=False,
font=dict(size=12, color="black"),
)
# Customization of layout and traces
fig.update_layout(template='simple_white', title='', yaxis_title='Title Y', barmode='stack',
dragmode='drawrect', font_size=12,
# style of new shapes
newshape=dict(line_color='magenta', fillcolor=None, opacity=0.5),
hoverlabel_namelength=-1)
fig.update_traces(marker_line_color='rgba(0,0,0,0.8)', marker_line_width=1, textfont_size=12, opacity=0.8)
#fig.update_shapes(opacity=1)
# Make figure zoomable, hide logo et cetera
config = dict({'scrollZoom':True, 'displaylogo': True,
'modeBarButtonsToAdd':['drawopenpath', 'eraseshape']
})
fig.show(config=config)
print(mean)
print(sem)