How to group data with color but still show a trendline for the entire dataset using Plotly Express?

Question:

I’m getting a trend line, however the issue is that with color and a trendline (set to max of data points), I’m getting multiple lines. I only want one line on the entire data set. Here is my code:

fig = px.scatter(df, "Date", y="Number", color="Technology", labels={
                     "Date": "Year",
                     "Number": "Number"
                 }, trendline="expanding", trendline_options=dict(function="max"))

enter image description here

Asked By: disruptive

||

Answers:

When you’re assigning different colors to different categories for a plotly express figure like you’ve done here with color="Technology, you can add the following to set the scope of the trendline to cover all data points and ignore the subcategories indicatd by color:

trendline_scope="overall"

Plot:

enter image description here

Complete code:

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", symbol="smoker", color="sex", trendline="ols", trendline_scope="overall")
fig.show()
Answered By: vestland
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.