{
"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 dowloading 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/), and [SciPy](https://www.scipy.org/)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"from plotly.tools import FigureFactory as FF\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import scipy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Average of 2 Curves"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given two curves defined by functions $f$ and $g$ on $\\mathbb{R} \\rightarrow \\mathbb{R}$, **the average curve** $h$ of $f$ and $g$ is defined by $h = \\frac{f(x) + g(x)}{2} $ for $x \\in \\mathbb{R}$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = np.linspace(0, 2*np.pi, 100)\n",
"f = np.sin(x)\n",
"g = np.cos(x)\n",
"h = [(f[j] + g[j])/2 for j in range(len(x))] \n",
"\n",
"trace1 = go.Scatter(\n",
" x=x,\n",
" y=f,\n",
" mode='lines',\n",
" name='f(x)',\n",
" marker=dict(\n",
" color='rgb(220, 20, 60)'\n",
" )\n",
")\n",
"\n",
"trace2 = go.Scatter(\n",
" x=x,\n",
" y=g,\n",
" mode='lines',\n",
" name='g(x)',\n",
" marker=dict(\n",
" color='rgb(100, 149, 237)'\n",
" )\n",
")\n",
"\n",
"trace3 = go.Scatter(\n",
" x=x,\n",
" y=h,\n",
" mode='markers+lines',\n",
" name='Average of f and g',\n",
" marker=dict(\n",
" color='rgb(128, 0, 128)',\n",
" symbol='diamond-open',\n",
" )\n",
")\n",
"\n",
"data = [trace1, trace2, trace3]\n",
"py.iplot(data, filename='2-curves')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"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 /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-CdzaoW-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 \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: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\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_Average_Multiple_Curves.ipynb', 'python/average_multiple_curves/', 'Average Multiple Curves | plotly',\n",
" 'Learn how to average the values of multiple curves with Python.',\n",
" title='Average Multiple Curves in Python | plotly',\n",
" name='Average Multiple Curves',\n",
" language='python',\n",
" page_type='example_index', has_thumbnail='false', display_as='mathematics', order=9,\n",
" ipynb= '~notebook_demo/107')"
]
},
{
"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
}