Black Lives Matter. Please consider donating to Black Girls Code today.

Styling scatter trendline in Plotly Express #Python

Plotly Express Scatter gives us the possibility to plot a trendline as well.
I would like to change the line width of this trendline, but I could not find a solution so far.

Thank you!


To style the trendline you have to find out which trace in your figure is a Scatter trace of mode='lines'


import as px
df =
fig = px.scatter(df, x="total_bill", y="tip", facet_row="time", facet_col="day", color="smoker", trendline="ols",
          category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
for  k, trace  in enumerate(
        if trace.mode is not None and trace.mode == 'lines':

 for id in tr_line:[id].update(line_width=4)

Prefect! :slight_smile:
Thanks you @empet

One more question regarding:

How to know if it is “line_width” or only “width” for example? Where can I see the attributes list?
Many thanks!

Hi @vcmorini,

line is a property for go.Scatter. It has width, 'color` and more attributes.
In low level Python you can set the width as follows:

line = {'width' : 2}

but starting with Plotly 4.0 the attributes can be written as line_width, line_color.

To get help on line print:


and you’ll get:

Help on property:

    The 'line' property is an instance of Line
    that may be specified as:
      - An instance of :class:`plotly.graph_objs.scatter.Line`
      - A dict of string/value properties that will be passed
        to the Line constructor
        Supported dict properties:
                Sets the line color.
                Sets the dash style of lines. Set to a dash
                type string ("solid", "dot", "dash",
                "longdash", "dashdot", or "longdashdot") or a
                dash length list in px (eg "5px,10px,2px,2px").
                Determines the line shape. With "spline" the
                lines are drawn using spline interpolation. The
                other available values correspond to step-wise
                line shapes.
                Simplifies lines by removing nearly-collinear
                points. When transitioning lines, it may be
                desirable to disable this so that the number of
                points along the resulting SVG path is
                Has an effect only if `shape` is set to
                "spline" Sets the amount of smoothing. 0
                corresponds to no smoothing (equivalent to a
                "linear" shape).
                Sets the line width (in px).

Great! That helped @empet :slight_smile: