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"))
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:
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()
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"))
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:
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()