Skip to content

Commit ff28870

Browse files
finally documenting px trendlines
1 parent d25e721 commit ff28870

File tree

2 files changed

+79
-125
lines changed

2 files changed

+79
-125
lines changed

python/linear-fits.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.1.1
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.8
24+
plotly:
25+
description: Add linear Ordinary Least Squares (OLS) regression trendlines or non-linear
26+
Locally Weighted Scatterplot Smoothing (LOEWSS) trendlines to scatterplots in Python.
27+
display_as: statistics
28+
language: python
29+
layout: base
30+
name: Linear and Non-Linear Trendlines
31+
order: 10
32+
page_type: u-guide
33+
permalink: python/linear-fits/
34+
thumbnail: thumbnail/linear_fit.jpg
35+
---
36+
37+
### Linear fit trendlines with Plotly Express
38+
39+
[Plotly Express](/python/plotly-express/) is the easy-to-use, high-level interface to Plotly, which [operates on "tidy" data](/python/px-arguments/).
40+
41+
Plotly Express allows you to add [Ordinary Least](https://en.wikipedia.org/wiki/Ordinary_least_squares) 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.
42+
43+
44+
```python
45+
import plotly.express as px
46+
47+
tips = px.data.tips()
48+
fig = px.scatter(tips, x="total_bill", y="tip", trendline="ols")
49+
fig.show()
50+
```
51+
52+
### Fitting multiple lines and retrieving the model parameters
53+
54+
Plotly Express will fit a trendline per trace, and allows you to access the underlying model parameters for all the models.
55+
56+
```python
57+
import plotly.express as px
58+
59+
tips = px.data.tips()
60+
fig = px.scatter(tips, x="total_bill", y="tip", facet_col="smoker", color="sex", trendline="ols")
61+
fig.show()
62+
63+
results = px.get_trendline_results(fig)
64+
print(results)
65+
66+
results.query("sex == 'Male' and smoker == 'Yes'").px_fit_results.iloc[0].summary()
67+
```
68+
69+
### Non-Linear Trendlines
70+
71+
Plotly Express also supports non-linear [LOWESS](https://en.wikipedia.org/wiki/Local_regression) trendlines.
72+
73+
```python
74+
import plotly.express as px
75+
76+
gapminder = px.data.gapminder().query("year == 2007")
77+
fig = px.scatter(gapminder, x="gdpPercap", y="lifeExp", color="continent", trendline="lowess")
78+
fig.show()
79+
```

unconverted/python/linear-fits.md

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)