{ "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": [ "#### Imports\n", "The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), [SciPy](https://www.scipy.org/) and [PeakUtils](http://pythonhosted.org/PeakUtils/)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.figure_factory as ff\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import scipy\n", "import peakutils" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tips\n", "Our method for finding the area under any peak is to find the area from the `data values` to the x-axis, the area from the `baseline` to the x-axis, and then take the difference between them. In particular, we want to find the areas of these functions defined on the x-axis interval $I$ under the peak.\n", "\n", "Let $T(x)$ be the function of the data, $B(x)$ the function of the baseline, and $Area$ the peak integration area between the baseline and the first peak. Since $T(x) \\geq B(x)$ for all $x$, then we know that\n", "\n", "$$\n", "\\begin{align}\n", "A = \\int_{I} T(x)dx - \\int_{I} B(x)dx \n", "\\end{align}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import Data\n", "For our example below we will import some data on milk production by month:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "milk_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/monthly-milk-production-pounds.csv')\n", "time_series = milk_data['Monthly milk production (pounds per cow)']\n", "time_series = np.asarray(time_series)\n", "\n", "df = milk_data[0:15]\n", "\n", "table = ff.create_table(df)\n", "py.iplot(table, filename='milk-production-dataframe')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Area Under One Peak" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/plotly/graph_objs/_deprecations.py:144: DeprecationWarning:\n", "\n", "plotly.graph_objs.Annotation is deprecated.\n", "Please replace it with one of the following more specific types\n", " - plotly.graph_objs.layout.Annotation\n", " - plotly.graph_objs.layout.scene.Annotation\n", "\n", "\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "baseline_values = peakutils.baseline(time_series)\n", "\n", "x = [j for j in range(len(time_series))]\n", "time_series = time_series.tolist()\n", "baseline_values = baseline_values.tolist()\n", "\n", "rev_baseline_values = baseline_values[:11]\n", "rev_baseline_values.reverse()\n", "area_x = [0,1,2,3,4,5,6,7,8,9,10,11,10,9,8,7,6,5,4,3,2,1]\n", "area_y = time_series[:11] + rev_baseline_values\n", "\n", "trace = go.Scatter(\n", " x=x,\n", " y=time_series,\n", " mode='lines',\n", " marker=dict(\n", " color='#B292EA',\n", " ),\n", " name='Original Plot'\n", ")\n", "\n", "trace2 = go.Scatter(\n", " x=x,\n", " y=baseline_values,\n", " mode='markers',\n", " marker=dict(\n", " size=3,\n", " color='#EB55BF',\n", " ),\n", " name='Bassline'\n", ")\n", "\n", "trace3 = go.Scatter(\n", " x=area_x,\n", " y=area_y,\n", " mode='lines+markers',\n", " marker=dict(\n", " size=4,\n", " color='rgb(255,0,0)',\n", " ),\n", " name='1st Peak Outline'\n", ")\n", "\n", "first_peak_x = [j for j in range(11)]\n", "area_under_first_peak = np.trapz(time_series[:11], first_peak_x) - np.trapz(baseline_values[:11], first_peak_x)\n", "area_under_first_peak\n", "\n", "annotation = go.Annotation(\n", " x=80,\n", " y=1000,\n", " text='The peak integration for the first peak is approximately %s' % (area_under_first_peak),\n", " showarrow=False\n", ")\n", "\n", "layout = go.Layout(\n", " annotations=[annotation]\n", ")\n", " \n", "trace_data = [trace, trace2, trace3]\n", "fig = go.Figure(data=trace_data, layout=layout)\n", "py.iplot(fig, filename='milk-production-peak-integration')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "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/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-iKhEAt\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-1Y3Gor/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "/Library/Frameworks/Python.framework/Versions/2.7/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", " 'python-Peak-Integration.ipynb', 'python/peak-integration/', 'Peak Integration | plotly',\n", " 'Learn how to integrate the area between peaks and bassline in Python.',\n", " title='Peak Integration in Python | plotly',\n", " name='Peak Integration',\n", " language='python',\n", " page_type='example_index', has_thumbnail='false', display_as='peak-analysis', order=4,\n", " ipynb= '~notebook_demo/121')" ] }, { "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.12" } }, "nbformat": 4, "nbformat_minor": 1 }