I am shading the confidence interval of a probability density function (PDF) and noticing that the update_layout(shapes) doesn’t allow shading of the Area Under the Curve (AUC). I am confused as to why this isn’t enabled by default. My environment has plotly==5.11.0 and dash==1.21.0.
Here is my code:
def hist_box(continuous):
# statistics measurements x_bar = continuous.mean() s = continuous.std() n = continuous.size se = s / np.sqrt(n) dof = n - 1 density = np.linspace(continuous.min(), continuous.max(), 100) pdf = norm.pdf(density, x_bar, s) ci = ta.interval(0.95, df=dof, loc=x_bar, scale=(s / np.sqrt(n))) # subplot 1/2 ff = go.Figure() ff.add_trace( go.Histogram( dict( x=continuous, histfunc="count", histnorm="probability density", marker={"color": "rgba(4, 217, 255, 255)"}, opacity=0.73, ), name=continuous.name.title() + " " ) ) ff.add_trace( go.Scatter( dict( x=density, y=pdf, mode="lines", marker={"color": "rgba(4, 217, 255, 255)"}, showlegend=False ) ) ) # subplot 2/2 fg = go.Figure() fg.add_trace( go.Box( x=continuous, showlegend=False, marker={"color": "rgba(4, 217, 255, 255)"}, name="" ) ) # final figure final_fig = make_subplots( rows=4, cols=1, specs=[ [{"type": "scatter", "rowspan": 3}], [None], [None], [{"type": "box", "rowspan": 1}] ], shared_xaxes="all" ) # add data to trace for t in ff.data: final_fig.add_trace(t, row=1, col=1) for t in fg.data: final_fig.add_trace(t, row=4, col=1) # layout fails, must put shape on rows, cols final_fig.update_layout( dict( font={"family": "Lato", "color": "#979797"}, paper_bgcolor="#111111", plot_bgcolor="#1e1e1e", title=dict( text=f"Distribution of {continuous.name.title()}", font={"size": 28.5, "color": "rgba(255, 255, 255, 155)"}, x=0.5, ), legend=dict( title=dict( text=" <b>Variable</b>"), x=0.01, y=0.97, bgcolor="#292929", font={"color": "#e1e1e1"} ), xaxis=dict( showticklabels=True, zeroline=False, showgrid=False ), yaxis=dict( title=dict( text="Probability Density"), zeroline=bool(False), showgrid=bool(False), range=[0, pdf.max() * 1.21], ), xaxis2=dict( showticklabels=False, zeroline=False, showgrid=False ), yaxis2=dict( zeroline=False, showgrid=False), ) ).add_shape( dict( line=dict(width=0), type="rect", xref="x1", yref="paper", fillcolor="rgba(167, 245, 233, 255)", x0=ci[0], x1=ci[1], y0=0, y1=pdf.max(), opacity=0.5, ), row=1, col=1 ) return final_fig
cont = x
final_fig = hist_box(cont)
plot(final_fig)
The resulting plot. The shaded region overshadows the PDF curve.
The ideal solution shades between x0 and x1, with y1 based on an existing trace.