fig = px.scatter(trump, x='Population_ln', y='Trump Share', trendline='ols',
labels={'Population_ln': 'Population (ln)'},
title='Relationship Between Population and Trump Share',
template='none')
fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False)
fig.show()
I have a scatter graph with an OLS trendline, and I’m trying to just change the color of the trendline to red. How can I do this? I could not find anything in the documentation…
Your fig consists in two traces: one for data points and another for trend line. First inspect which Scatter trace has mode=‘lines’ (although we know that px.line defines it as the second one)
for d in fig.data:
print(d['mode'])
Then update the line trace as follows:
fig.data[1].update(line_color='your new color code')
You can also use the approach in Plotly Express Trendlines, just the Lines to target other properties than the line color. There happens to be a dedicated keyword argument for this one but using .update_traces() you can batch-update any property of any trace matching any selector