{ "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: exponential 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": [ "### Exponential Fit" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'https://plotly.com/~PythonPlotBot/164'" ] }, "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", "from scipy.optimize import curve_fit\n", "\n", "\n", "x = np.array([399.75, 989.25, 1578.75, 2168.25, 2757.75, 3347.25, 3936.75, 4526.25, 5115.75, 5705.25])\n", "y = np.array([109,62,39,13,10,4,2,0,1,2])\n", "\n", "def exponenial_func(x, a, b, c):\n", " return a*np.exp(-b*x)+c\n", "\n", "\n", "popt, pcov = curve_fit(exponenial_func, x, y, p0=(1, 1e-6, 1))\n", "\n", "xx = np.linspace(300, 6000, 1000)\n", "yy = exponenial_func(xx, *popt)\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=xx, \n", " y=yy, \n", " mode='lines',\n", " marker=go.Marker(color='rgb(31, 119, 180)'),\n", " name='Fit'\n", " )\n", "\n", "annotation = go.Annotation(\n", " x=2000,\n", " y=100,\n", " text='$\\textbf{Fit}: 163.56e^{-0.00097x} - 1.16$',\n", " showarrow=False\n", " )\n", "layout = go.Layout(\n", " title='Exponential 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='Exponential-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-w7cm96iz-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", " 'Exponential-fits.ipynb', 'python/exponential-fits/', 'Exponential Fit',\n", " 'Create a exponential fit / regression in Python and add a line of best fit to your chart.',\n", " title = 'Exponential Fit',\n", " name = 'Exponential Fit',\n", " has_thumbnail='true', thumbnail='thumbnail/exponential_fit.jpg', \n", " language='python', page_type='example_index',\n", " display_as='statistics', order=11,\n", " ipynb= '~notebook_demo/135')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "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 }