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:
Thanks in advance!
With the release of Plotly 5.2.1 (2021-08-13)
using px.scatter()
lets you specify:
trendline_scope = 'overall'
trendline_scope = 'overall'
If the greenish color of the trendline is not to your liking, you can change that through:
trendline_color_override = 'black'
trendline_color_override = 'black'
The other option for trendline_scope
is trace
which produces:
trendline_scope = 'trace'
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()
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:
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:
Change in code: Just add trendline='ols'
fig = px.scatter(
x=years,
y=value,
trendline='ols',
color=colors
)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With