How to have just one trendline for multiple colors in plotly express scatter?
Asked Answered
V

2

8

I want to create a scatter plot with only one trendline. Plotly express creates a different trendline for each color in the points list.

import plotly.express as px

value = [15, 20, 35, 40, 48]
years = [2010, 2011, 2012, 2013, 2014]
colors = ['red', 'red', 'blue', 'blue', 'blue']

fig = px.scatter(
    x=years,
    y=value,
    trendline='ols',
    color=colors
)

fig.show()

Is there a way to create just one trendline for all the points?

Plot:

enter image description here

Thanks in advance!

Virgy answered 24/10, 2019 at 16:11 Comment(0)
B
2

There's no built-in feature for this at the moment, no, unfortunately! But it's a good idea and I've created an issue to suggest it as an addition: https://github.com/plotly/plotly.py/issues/1846

Burnsed answered 24/10, 2019 at 16:32 Comment(0)
E
8

With the release of Plotly 5.2.1 (2021-08-13)using px.scatter() lets you specify:

trendline_scope = 'overall'

Plot 1 - trendline_scope = 'overall'

enter image description here

If the greenish color of the trendline is not to your liking, you can change that through:

 trendline_color_override = 'black'

Plot 2 - trendline_color_override = 'black'

enter image description here

The other option for trendline_scopeis trace which produces:

Plot 3 - trendline_scope = 'trace'

enter image description here

Complete code:

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip",
                 color="sex",
                 trendline="ols",
                 trendline_scope = 'overall',
#                trendline_scope = 'trace'
                 trendline_color_override = 'black'
                )
fig.show()

Previous answer for older versions:


Since you're not specifically asking for a built-in plotly express feature, you can easily build on px.Scatter() and obtain what you want using statsmodels.OLS together with add_traces(go.Scatter()):

Plot:

enter image description here

Code:

import plotly.express as px
import plotly.graph_objs as go
import statsmodels.api as sm

value = [15, 20, 35, 40, 48]
years = [2010, 2011, 2012, 2013, 2014]
colors = ['red', 'red', 'blue', 'blue', 'blue']

# your original setup
fig = px.scatter(
    x=years,
    y=value,
    color=colors
)

# linear regression
regline = sm.OLS(value,sm.add_constant(years)).fit().fittedvalues

# add linear regression line for whole sample
fig.add_traces(go.Scatter(x=years, y=regline,
                          mode = 'lines',
                          marker_color='black',
                          name='trend all')
                          )
fig

And you can have it both ways:

Plot:

enter image description here

Change in code: Just add trendline='ols'

fig = px.scatter(
    x=years,
    y=value,
    trendline='ols',
    color=colors
)
Entreaty answered 25/10, 2019 at 8:3 Comment(2)
This will work until the feature is created in plotly.express ;)Virgy
@Virgy A good plan today is better than a perfect plan tomorrow =)Entreaty
B
2

There's no built-in feature for this at the moment, no, unfortunately! But it's a good idea and I've created an issue to suggest it as an addition: https://github.com/plotly/plotly.py/issues/1846

Burnsed answered 24/10, 2019 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.