{
"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",
"#### Version Check\n",
"Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.1.1'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Basic Overlaid Area Chart"
]
},
{
"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",
"trace1 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[0, 2, 3, 5],\n",
" fill='tozeroy'\n",
")\n",
"trace2 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[3, 5, 1, 7],\n",
" fill='tonexty'\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"py.iplot(data, filename='basic-area')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Overlaid Area Chart Without Boundary Lines"
]
},
{
"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",
"trace1 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[0, 2, 3, 5],\n",
" fill='tozeroy',\n",
" mode= 'none'\n",
")\n",
"trace2 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[3, 5, 1, 7],\n",
" fill='tonexty',\n",
" mode= 'none'\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"py.iplot(data, filename='basic-area-no-bound')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Interior Filling for Area Chart"
]
},
{
"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",
"trace0 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[3, 4, 8, 3],\n",
" fill= None,\n",
" mode='lines',\n",
" line=dict(\n",
" color='rgb(143, 19, 131)',\n",
" )\n",
")\n",
"trace1 = go.Scatter(\n",
" x=[1, 2, 3, 4],\n",
" y=[1, 6, 2, 6],\n",
" fill='tonexty',\n",
" mode='lines',\n",
" line=dict(\n",
" color='rgb(143, 19, 131)',\n",
" )\n",
")\n",
"\n",
"data = [trace0, trace1]\n",
"py.iplot(data, filename='filling-interior-area')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Stacked Area Chart"
]
},
{
"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",
"# Add original data\n",
"x=['Winter', 'Spring', 'Summer', 'Fall']\n",
"\n",
"trace0 = dict(\n",
" x=x,\n",
" y=[40, 60, 40, 10],\n",
" hoverinfo='x+y',\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(131, 90, 241)'),\n",
" stackgroup='one'\n",
")\n",
"trace1 = dict(\n",
" x=x,\n",
" y=[20, 10, 10, 60],\n",
" hoverinfo='x+y',\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(111, 231, 219)'),\n",
" stackgroup='one'\n",
")\n",
"trace2 = dict(\n",
" x=x,\n",
" y=[40, 30, 50, 30],\n",
" hoverinfo='x+y',\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(184, 247, 212)'),\n",
" stackgroup='one'\n",
")\n",
"data = [trace0, trace1, trace2]\n",
"\n",
"fig = dict(data=data)\n",
"py.iplot(fig, filename='stacked-area-plot-hover', validate=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stacked Area Chart with Normalized Values"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = dict(\n",
" x=['Winter', 'Spring', 'Summer', 'Fall'],\n",
" y=['40', '20', '30', '40'],\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(184, 247, 212)'),\n",
" stackgroup='one',\n",
" groupnorm='percent'\n",
")\n",
"trace1 = dict(\n",
" x=['Winter', 'Spring', 'Summer', 'Fall'],\n",
" y=['50', '70', '40', '60'],\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(111, 231, 219)'),\n",
" stackgroup='one'\n",
")\n",
"trace2 = dict(\n",
" x=['Winter', 'Spring', 'Summer', 'Fall'],\n",
" y=['70', '80', '60', '70'],\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(127, 166, 238)'),\n",
" stackgroup='one'\n",
")\n",
"trace3 = dict(\n",
" x=['Winter', 'Spring', 'Summer', 'Fall'],\n",
" y=['100', '100', '100', '100'],\n",
" mode='lines',\n",
" line=dict(width=0.5,\n",
" color='rgb(131, 90, 241)'),\n",
" stackgroup='one'\n",
")\n",
"data = [trace0, trace1, trace2, trace3]\n",
"layout = go.Layout(\n",
" showlegend=True,\n",
" xaxis=dict(\n",
" type='category',\n",
" ),\n",
" yaxis=dict(\n",
" type='linear',\n",
" range=[1, 100],\n",
" dtick=20,\n",
" ticksuffix='%'\n",
" )\n",
")\n",
"fig = dict(data=data, layout=layout)\n",
"py.iplot(fig, filename='stacked-area-plot-norm', validate=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Select Hover Points"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Scatter(\n",
" x=[0,0.5,1,1.5,2],\n",
" y=[0,1,2,1,0],\n",
" fill= 'toself',\n",
" fillcolor = '#ab63fa',\n",
" hoveron = 'points+fills',\n",
" line = dict(\n",
" color = '#ab63fa'\n",
" ),\n",
" text = \"Points + Fills\",\n",
" hoverinfo = 'text'\n",
")\n",
"\n",
"trace1 = go.Scatter(\n",
" x=[3,3.5,4,4.5,5],\n",
" y=[0,1,2,1,0],\n",
" fill='toself',\n",
" fillcolor = '#e763fa',\n",
" hoveron = 'points',\n",
" line = dict(\n",
" color = '#e763fa'\n",
" ),\n",
" text = \"Points only\",\n",
" hoverinfo = 'text'\n",
")\n",
"\n",
"data = [trace0, trace1]\n",
"\n",
"layout = go.Layout(\n",
" title = \"hover on points or fill\",\n",
" xaxis = dict(\n",
" range = [0,5.2]\n",
" ),\n",
" yaxis = dict(\n",
" range = [0,3]\n",
" )\n",
")\n",
"\n",
"fig = go.Figure(data=data,layout=layout)\n",
"py.iplot(data, filename='select-hover-points')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#scatter-line\n",
"and https://plotly.com/python/reference/#scatter-fill \n",
"for more information and attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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 /tmp/pip-req-build-ewWF0_\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-zaYWOc/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",
"\u001b[33mYou are using pip version 10.0.1, however version 18.0 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",
" 'area.ipynb', 'python/filled-area-plots/', 'Filled Area Plots | plotly',\n",
" 'How to make filled area plots in Python with Plotly.',\n",
" title = 'Filled Area Plots | plotly',\n",
" name = 'Filled Area Plots',\n",
" thumbnail='thumbnail/area.jpg', language='python',\n",
" has_thumbnail='true', display_as='basic', order=3.5,\n",
" ipynb='~notebook_demo/8')"
]
},
{
"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.15rc1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}