CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

How to fill in the area below trendline in plotly express scatterplot?

I’ve drawn a scatterplot with a non-linear trendline and a vertical line:

import plotly.express as px
uk_age = df[df['CountryLive']=='United Kingdom']['Age'].mean()

fig = px.scatter(df, x=x, y=y, trendline="lowess", trendline_options=dict(frac=0.4), 
                 trendline_color_override="red",template='plotly_white', range_y=[0,30])
fig.add_vline(x=uk_age, annotation_text="UK", 
              annotation_position="top right", line_color="blue")
fig.show()
  1. I would like to fill the area below the trendline.
  2. I would like to fill the area below the trendline, but only to the right of the
    vline (like on the drawing below, apologies for my art skills)

desired output

I’ve played around with extracting the trendline results and plotting them again, but couldn’t fill them. The fact that it’s a smoothened trendline(frac=0.4), not a ‘normal’ plot line makes it a difficult task. Any ideas?

you can use the parameters fillcolor and set the color opacity to shade out.
go.Scatter(x=x, y=y, 
                         line_color='#333',
                         fillcolor='grey',
                         opacity=0.1,
                         fill='tozeroy')

that doesn’t do the trick, a while ago I got the solution from stackoverflow: link
the fact that, the trendline is smothened makes the whole task a lot harder