{ "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: Polynomial 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": [ "### Polynomial Fit" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 3 2\n", "0.4345 x - 5.607 x + 16.78 x - 10.61\n" ] }, { "data": { "text/plain": [ "u'https://plotly.com/~PythonPlotBot/166'" ] }, "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", "import numpy as np\n", "\n", "points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])\n", "\n", "# get x and y vectors\n", "x = points[:,0]\n", "y = points[:,1]\n", "\n", "# calculate polynomial\n", "z = np.polyfit(x, y, 3)\n", "f = np.poly1d(z)\n", "print f\n", "\n", "# calculate new x's and y's\n", "x_new = np.linspace(x[0], x[-1], 50)\n", "y_new = f(x_new)\n", "\n", "# Creating the dataset, and generating the plot\n", "trace1 = go.Scatter(\n", " x=x,\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=x_new,\n", " y=y_new,\n", " mode='lines',\n", " marker=go.Marker(color='rgb(31, 119, 180)'),\n", " name='Fit'\n", " )\n", "\n", "annotation = go.Annotation(\n", " x=6,\n", " y=-4.5,\n", " text='$\\textbf{Fit}: 0.43X^3 - 0.56X^2 + 16.78X + 10.61$',\n", " showarrow=False\n", " )\n", "layout = go.Layout(\n", " title='Polynomial 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='Polynomial-Fit-in-python')" ] }, { "cell_type": "code", "execution_count": 3, "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-rdh9pziz-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" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n", "\n", "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\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", " 'Polynomial-fits.ipynb', 'python/polynomial-fits/', 'Polynomial Fit',\n", " 'Create a polynomial fit / regression in Python and add a line of best fit to your chart.',\n", " title = 'Polynomial Fit',\n", " name = 'Polynomial Fit',\n", " has_thumbnail='true', thumbnail='thumbnail/polynomial_fit.jpg', \n", " language='python', page_type='example_index',\n", " display_as='statistics', order=12,\n", " ipynb= '~notebook_demo/138')" ] }, { "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 }