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.
Ref: https://plotly.com/python/plotly-express/

Thank you!

@vcmorini,

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

Example:

import plotly.express as px
df = px.data.tips()
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"]})
fig.show()
tr_line=[]
for  k, trace  in enumerate(fig.data):
        if trace.mode is not None and trace.mode == 'lines':
            tr_line.append(k)
print(tr_line)

 for id in tr_line:
    fig.data[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:

help(go.Scatter.line)

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:
            
            color
                Sets the line color.
            dash
                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").
            shape
                Determines the line shape. With "spline" the
                lines are drawn using spline interpolation. The
                other available values correspond to step-wise
                line shapes.
            simplify
                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
                unaffected.
            smoothing
                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).
            width
                Sets the line width (in px).
    
    Returns
    -------
    plotly.graph_objs.scatter.Line

Great! That helped @empet :slight_smile: