jupyter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.
Plotly Express allows you to add Ordinary Least Squares regression trendline to scatterplots with the trendline
argument. In order to do so, you will need to install statsmodels
and its dependencies. Hovering over the trendline will show the equation of the line and its R-squared value.
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="ols")
fig.show()
Plotly Express will fit a trendline per trace, and allows you to access the underlying model parameters for all the models.
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_col="smoker", color="sex", trendline="ols")
fig.show()
results = px.get_trendline_results(fig)
print(results)
results.query("sex == 'Male' and smoker == 'Yes'").px_fit_results.iloc[0].summary()
Plotly Express also supports non-linear LOWESS trendlines.
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent", trendline="lowess")
fig.show()