{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Version Check\n",
"Note: Linear fits are available in version 1.9.2+
\n",
"Run `pip install plotly --upgrade` to update your Plotly version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1.12.12'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Linear Fit"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"u'https://plotly.com/~PythonPlotBot/162'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Learn about API authentication here: https://plotly.com/python/getting-started\n",
"# Find your api_key here: https://plotly.com/settings/api\n",
"\n",
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"# Scientific libraries\n",
"from numpy import arange,array,ones\n",
"from scipy import stats\n",
"\n",
"\n",
"xi = arange(0,9)\n",
"A = array([ xi, ones(9)])\n",
"\n",
"# (Almost) linear sequence\n",
"y = [19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]\n",
"\n",
"# Generated linear fit\n",
"slope, intercept, r_value, p_value, std_err = stats.linregress(xi,y)\n",
"line = slope*xi+intercept\n",
"\n",
"# Creating the dataset, and generating the plot\n",
"trace1 = go.Scatter(\n",
" x=xi, \n",
" y=y, \n",
" mode='markers',\n",
" marker=go.Marker(color='rgb(255, 127, 14)'),\n",
" name='Data'\n",
" )\n",
"\n",
"trace2 = go.Scatter(\n",
" x=xi, \n",
" y=line, \n",
" mode='lines',\n",
" marker=go.Marker(color='rgb(31, 119, 180)'),\n",
" name='Fit'\n",
" )\n",
"\n",
"annotation = go.Annotation(\n",
" x=3.5,\n",
" y=23.5,\n",
" text='$R^2 = 0.9551,\\\\Y = 0.716X + 19.18$',\n",
" showarrow=False,\n",
" font=go.Font(size=16)\n",
" )\n",
"layout = go.Layout(\n",
" title='Linear Fit in Python',\n",
" plot_bgcolor='rgb(229, 229, 229)',\n",
" xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),\n",
" yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'),\n",
" annotations=[annotation]\n",
" )\n",
"\n",
"data = [trace1, trace2]\n",
"fig = go.Figure(data=data, layout=layout)\n",
"\n",
"py.plot(fig, filename='Linear-Fit-in-python')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /private/var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-yrr9lh6f-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.10\n",
" Uninstalling publisher-0.10:\n",
" Successfully uninstalled publisher-0.10\n",
" Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \b|\b \bdone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'linear-fits.ipynb', 'python/linear-fits/', 'Linear Fit',\n",
" 'Create a linear fit / regression in Python and add a line of best fit to your chart.',\n",
" title = 'Linear Fit',\n",
" name = 'Linear Fit',\n",
" has_thumbnail='true', thumbnail='thumbnail/linear_fit.jpg', \n",
" language='python', page_type='example_index',\n",
" display_as='statistics', order=10,\n",
" ipynb= '~notebook_demo/139')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}