{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/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": [
"#### 3D Line Plot of Brownian Motion"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris.csv')\n",
"df.head()\n",
"\n",
"def brownian_motion(T = 1, N = 100, mu = 0.1, sigma = 0.01, S0 = 20):\n",
" dt = float(T)/N\n",
" t = np.linspace(0, T, N)\n",
" W = np.random.standard_normal(size = N)\n",
" W = np.cumsum(W)*np.sqrt(dt) # standard brownian motion\n",
" X = (mu-0.5*sigma**2)*t + sigma*W\n",
" S = S0*np.exp(X) # geometric brownian motion\n",
" return S\n",
"\n",
"dates = pd.date_range('2012-01-01', '2013-02-22')\n",
"T = (dates.max()-dates.min()).days / 365\n",
"N = dates.size\n",
"start_price = 100\n",
"y = pd.Series(\n",
" brownian_motion(T, N, sigma=0.1, S0=start_price), index=dates)\n",
"z = pd.Series(\n",
" brownian_motion(T, N, sigma=0.1, S0=start_price), index=dates)\n",
"\n",
"trace = go.Scatter3d(\n",
" x=list(dates), y=y, z=z,\n",
" marker=dict(\n",
" size=4,\n",
" color=z,\n",
" colorscale='Viridis',\n",
" ),\n",
" line=dict(\n",
" color='#1f77b4',\n",
" width=1\n",
" )\n",
")\n",
"\n",
"data = [trace]\n",
"\n",
"layout = dict(\n",
" width=800,\n",
" height=700,\n",
" autosize=False,\n",
" title='Iris dataset',\n",
" scene=dict(\n",
" xaxis=dict(\n",
" gridcolor='rgb(255, 255, 255)',\n",
" zerolinecolor='rgb(255, 255, 255)',\n",
" showbackground=True,\n",
" backgroundcolor='rgb(230, 230,230)'\n",
" ),\n",
" yaxis=dict(\n",
" gridcolor='rgb(255, 255, 255)',\n",
" zerolinecolor='rgb(255, 255, 255)',\n",
" showbackground=True,\n",
" backgroundcolor='rgb(230, 230,230)'\n",
" ),\n",
" zaxis=dict(\n",
" gridcolor='rgb(255, 255, 255)',\n",
" zerolinecolor='rgb(255, 255, 255)',\n",
" showbackground=True,\n",
" backgroundcolor='rgb(230, 230,230)'\n",
" ),\n",
" camera=dict(\n",
" up=dict(\n",
" x=0,\n",
" y=0,\n",
" z=1\n",
" ),\n",
" eye=dict(\n",
" x=-1.7428,\n",
" y=1.0707,\n",
" z=0.7100,\n",
" )\n",
" ),\n",
" aspectratio = dict( x=1, y=1, z=0.7 ),\n",
" aspectmode = 'manual'\n",
" ),\n",
")\n",
"\n",
"fig = dict(data=data, layout=layout)\n",
"\n",
"py.iplot(fig, filename='pandas-brownian-motion-3d', height=700)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plot.ly/python/reference/#scatter3d-marker-line for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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-bub8Xo\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-9TZlql/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",
" \n",
"import publisher\n",
"publisher.publish(\n",
" '3d-line.ipynb', 'python/3d-line-plots/', 'Python 3D Line Plots | plotly',\n",
" 'How to make 3D Line Plots',\n",
" title= '3D Line Plots in Python | plotly',\n",
" name = '3D Line Plots',\n",
" has_thumbnail='true', thumbnail='thumbnail/3d-line.jpg', \n",
" language='python',\n",
" display_as='3d_charts', order=3,\n",
" ipynb= '~notebook_demo/63')"
]
},
{
"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
}