Show Sidebar Hide Sidebar # Linear Fit in Python

Create a linear fit / regression in Python and add a line of best fit to your chart.

#### New to Plotly?¶

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

#### Version Check¶

Note: Linear fits are available in version 1.9.2+
Run pip install plotly --upgrade to update your Plotly version

In :
import plotly
plotly.__version__

Out:
'1.12.12'

### Linear Fit¶

In :
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

# Scientific libraries
from numpy import arange,array,ones
from scipy import stats

xi = arange(0,9)
A = array([ xi, ones(9)])

# (Almost) linear sequence
y = [19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]

# Generated linear fit
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,y)
line = slope*xi+intercept

# Creating the dataset, and generating the plot
trace1 = go.Scatter(
x=xi,
y=y,
mode='markers',
marker=go.Marker(color='rgb(255, 127, 14)'),
name='Data'
)

trace2 = go.Scatter(
x=xi,
y=line,
mode='lines',
marker=go.Marker(color='rgb(31, 119, 180)'),
name='Fit'
)

annotation = go.Annotation(
x=3.5,
y=23.5,
text='$R^2 = 0.9551,\\Y = 0.716X + 19.18$',
showarrow=False,
font=go.Font(size=16)
)
layout = go.Layout(
title='Linear Fit in Python',
plot_bgcolor='rgb(229, 229, 229)',
xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),
yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),
annotations=[annotation]
)

data = [trace1, trace2]
fig = go.Figure(data=data, layout=layout)

py.plot(fig, filename='Linear-Fit-in-python')

Out:
u'https://plot.ly/~PythonPlotBot/162' 