Search code examples
pythonplotlyregressionplotly-express

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


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!


Solution

  • 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