{
"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: Animations are available in version 1.12.10+\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": [
"#### Import Data\n",
"Let us import some apple stock data for this animation."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"from plotly.grid_objs import Grid, Column\n",
"from plotly.tools import FigureFactory as FF \n",
"\n",
"import time\n",
"from datetime import datetime\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"appl = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')\n",
"appl.columns = [col.replace('AAPL.', '') for col in appl.columns]\n",
"apple_data_matrix = appl.head(10).round(2)\n",
"\n",
"table = FF.create_table(apple_data_matrix)\n",
"py.iplot(table, filename='apple_data_table')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Make the Grid"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'https://plotly.com/~PythonPlotBot/3031/'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def to_unix_time(dt):\n",
" epoch = datetime.utcfromtimestamp(0)\n",
" return (dt - epoch).total_seconds() * 1000\n",
"\n",
"appl_price = list(appl['Adjusted'])\n",
"my_columns = []\n",
"for k in range(len(appl.Date) - 1):\n",
" my_columns.append(Column(list(appl.Date)[:k + 1], 'x{}'.format(k + 1))) \n",
" my_columns.append(Column(appl_price[:k + 1], 'y{}'.format(k + 1)))\n",
"grid = Grid(my_columns)\n",
"py.grid_ops.upload(grid, 'AAPL-daily-stock-price' + str(time.time()), auto_open=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Make the Figure"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data=[dict(type='scatter',\n",
" xsrc=grid.get_column_reference('x1'),\n",
" ysrc= grid.get_column_reference('y1'),\n",
" name='AAPL',\n",
" mode='lines',\n",
" line=dict(color= 'rgb(114, 186, 59)'),\n",
" fill='tozeroy',\n",
" fillcolor='rgba(114, 186, 59, 0.5)')]\n",
"\n",
"axis=dict(ticklen=4,\n",
" mirror=True,\n",
" zeroline=False,\n",
" showline=True,\n",
" autorange=False,\n",
" showgrid=False)\n",
"\n",
"layout = dict(title='AAPL Daily Stock Price',\n",
" font=dict(family='Balto'),\n",
" showlegend=False,\n",
" autosize=False,\n",
" width=800,\n",
" height=400,\n",
" xaxis=dict(axis, **{'nticks':12, 'tickangle':-45,\n",
" 'range': [to_unix_time(datetime(2015, 2, 17)),\n",
" to_unix_time(datetime(2016, 11, 30))]}),\n",
" yaxis=dict(axis, **{'title': '$', 'range':[0,170]}),\n",
" updatemenus=[dict(type='buttons',\n",
" showactive=False,\n",
" y=1,\n",
" x=1.1,\n",
" xanchor='right',\n",
" yanchor='top',\n",
" pad=dict(t=0, r=10),\n",
" buttons=[dict(label='Play',\n",
" method='animate',\n",
" args=[None, dict(frame=dict(duration=50, redraw=False), \n",
" transition=dict(duration=0),\n",
" fromcurrent=True,\n",
" mode='immediate')])])])\n",
"\n",
"frames=[{'data':[{'xsrc': grid.get_column_reference('x{}'.format(k + 1)),\n",
" 'ysrc': grid.get_column_reference('y{}'.format(k + 1))}],\n",
" 'traces': [0]\n",
" } for k in range(len(appl.Date) - 1)]\n",
"\n",
"fig=dict(data=data, layout=layout, frames=frames)\n",
"py.icreate_animations(fig, 'AAPL-stockprice' + str(time.time()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"For additional information on filled area plots in Plotly see: https://plotly.com/python/filled-area-plots/.\n",
"For more documentation on creating animations with Plotly, see https://plotly.com/python/#animations."
]
},
{
"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 /tmp/pip-req-build-obc71o_a\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-_a4gf6by/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.13\n",
" Uninstalling publisher-0.13:\n",
" Successfully uninstalled publisher-0.13\n",
"Successfully installed publisher-0.13\n",
"\u001b[33mYou are using pip version 10.0.1, however version 18.1 is available.\n",
"You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\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",
" 'filled-area-animation.ipynb', 'python/filled-area-animation/', 'Filled-Area Animation | plotly',\n",
" 'How to make an animated filled-area plot with apple stock data in Python.',\n",
" title='Filled-Area Animation | plotly',\n",
" name='Filled-Area Animation',\n",
" language='python',\n",
" page_type='example_index', has_thumbnail='true', thumbnail='thumbnail/apple_stock_animation.gif',\n",
" display_as='animations', ipynb= '~notebook_demo/128', order=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}