Fill (color) area under density plot

Hi plotly community,

I have created the following density plot:

using the following code:

import numpy as np
import plotly.figure_factory as ff

data = np.random.normal(0, 1, 1000)
name = "Example"

fig = ff.create_distplot([data], [name], show_hist=False, show_rug=False)
fig = fig.add_vline(
    x=np.quantile(data, q=0.5),
    line_width=3,
    line_dash="dash",
    line_color="black",
    annotation_text=f"Median: {round(np.quantile(data, q=0.5))}",
    annotation_position="top right",
    annotation_font_size=12,
    annotation_font_color="black",
)
fig = fig.add_vline(
    x=np.quantile(data, q=0.05),
    line_width=3,
    line_dash="dash",
    line_color="red",
    annotation_text=f"5% Quantil: {round(np.quantile(data, q=.05))}",
    annotation_position="bottom right",
    annotation_font_size=12,
    annotation_font_color="red",
)
fig = fig.add_vline(
    x=np.quantile(data, q=0.95),
    line_width=3,
    line_dash="dash",
    line_color="green",
    annotation_text=f"95% Quantil: {round(np.quantile(data, q=.95))}",
    annotation_position="bottom right",
    annotation_font_size=12,
    annotation_font_color="green",
)
fig.update_layout(plot_bgcolor="white")
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='lightgrey')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='lightgrey')
fig.update_layout(showlegend=False)
fig.update_layout(title=name)

fig

Now I would like to fill the area under the density curve and left from the red dotted line with red color and the area under the density curve right to the green dotted line with green color. But somehow I am totally lost doing it. Does anybody know how to fill those areas with color?

Thanks a lot!

Best,
chiquadrat

@chiquadrat

To get the two filled areas add to your fig two more scatter plots that fill the regions of interest:

xl = np.quantile(data, q=0.05)
xr = np.quantile(data, q=0.95)
x1   = [xc   for xc in fig.data[0].x if xc <xl]
y1   = fig.data[0].y[:len(x1)]

x2   = [xc   for xc in fig.data[0].x if xc > xr]
y2   = fig.data[0].y[-len(x2):]
fig.add_scatter(x=x1, y=y1,fill='tozeroy', mode='none' , fillcolor='red')
fig.add_scatter(x=x2, y=y2,fill='tozeroy', mode='none' , fillcolor='green')

I changed the position of the second annotation to
annotation_position="bottom left"

Nice thanks, this has helped me a lot!