{
"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!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Time Series Plot with `datetime` Objects ###"
]
},
{
"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",
"\n",
"import pandas as pd\n",
"from datetime import datetime\n",
"\n",
"df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')\n",
"\n",
"data = [go.Scatter(x=df.Date, y=df['AAPL.High'])]\n",
"\n",
"py.iplot(data, filename = 'time-series-simple')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Date Strings"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv\")\n",
"\n",
"data = [go.Scatter(\n",
" x=df.Date,\n",
" y=df['AAPL.Close'])]\n",
"\n",
"py.iplot(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Time Series Plot with Custom Date Range "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import datetime\n",
"\n",
"def to_unix_time(dt):\n",
" epoch = datetime.datetime.utcfromtimestamp(0)\n",
" return (dt - epoch).total_seconds() * 1000\n",
"\n",
"x = [datetime.datetime(year=2013, month=10, day=04),\n",
" datetime.datetime(year=2013, month=11, day=05),\n",
" datetime.datetime(year=2013, month=12, day=06)]\n",
"data = [go.Scatter(\n",
" x=x,\n",
" y=[1, 3, 6])]\n",
"\n",
"layout = go.Layout(xaxis = dict(\n",
" range = [to_unix_time(datetime.datetime(2013, 10, 17)),\n",
" to_unix_time(datetime.datetime(2013, 11, 20))]\n",
" ))\n",
"\n",
"fig = go.Figure(data = data, layout = layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Manually Set Range"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv\")\n",
"\n",
"trace_high = go.Scatter(\n",
" x=df.Date,\n",
" y=df['AAPL.High'],\n",
" name = \"AAPL High\",\n",
" line = dict(color = '#17BECF'),\n",
" opacity = 0.8)\n",
"\n",
"trace_low = go.Scatter(\n",
" x=df.Date,\n",
" y=df['AAPL.Low'],\n",
" name = \"AAPL Low\",\n",
" line = dict(color = '#7F7F7F'),\n",
" opacity = 0.8)\n",
"\n",
"data = [trace_high,trace_low]\n",
"\n",
"layout = dict(\n",
" title = \"Manually Set Date Range\",\n",
" xaxis = dict(\n",
" range = ['2016-07-01','2016-12-31'])\n",
")\n",
"\n",
"fig = dict(data=data, layout=layout)\n",
"py.iplot(fig, filename = \"Manually Set Range\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Time Series With Rangeslider"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv\")\n",
"\n",
"trace_high = go.Scatter(\n",
" x=df.Date,\n",
" y=df['AAPL.High'],\n",
" name = \"AAPL High\",\n",
" line = dict(color = '#17BECF'),\n",
" opacity = 0.8)\n",
"\n",
"trace_low = go.Scatter(\n",
" x=df.Date,\n",
" y=df['AAPL.Low'],\n",
" name = \"AAPL Low\",\n",
" line = dict(color = '#7F7F7F'),\n",
" opacity = 0.8)\n",
"\n",
"data = [trace_high,trace_low]\n",
"\n",
"layout = dict(\n",
" title='Time Series with Rangeslider',\n",
" xaxis=dict(\n",
" rangeselector=dict(\n",
" buttons=list([\n",
" dict(count=1,\n",
" label='1m',\n",
" step='month',\n",
" stepmode='backward'),\n",
" dict(count=6,\n",
" label='6m',\n",
" step='month',\n",
" stepmode='backward'),\n",
" dict(step='all')\n",
" ])\n",
" ),\n",
" rangeslider=dict(\n",
" visible = True\n",
" ),\n",
" type='date'\n",
" )\n",
")\n",
"\n",
"fig = dict(data=data, layout=layout)\n",
"py.iplot(fig, filename = \"Time Series with Rangeslider\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dash Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-timeseriesplot) can easily be deployed to a PaaS."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-timeseriesplot/\", width=\"100%\", height=\"750px\", frameBorder=\"0\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-timeseriesplot/code\", width=\"100%\", height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#layout-xaxis-rangeslider and
https://plotly.com/python/reference/#layout-xaxis-rangeselector for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-mtlz90v1\n",
"Building wheels for collected packages: publisher\n",
" Building wheel for publisher (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-mkvkhtzs/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 19.0.3, however version 19.1.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",
"\n",
"import publisher\n",
"publisher.publish(\n",
" 'time-series.ipynb', 'python/time-series/', 'Python Time Series | Examples | Plotly',\n",
" 'How to plot date and time in python. ',\n",
" title= 'Time Series Plots | plotly',\n",
" name = 'Time Series',\n",
" has_thumbnail='true', thumbnail='thumbnail/time-series.jpg', \n",
" language='python', page_type='example_index', \n",
" display_as='financial', order=0,\n",
" ipynb='~notebook_demo/213')"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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
}