{
"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": [
"'2.7.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Basic Bar 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",
"data = [go.Bar(\n",
" x=['giraffes', 'orangutans', 'monkeys'],\n",
" y=[20, 14, 23]\n",
" )]\n",
"\n",
"py.iplot(data, filename='basic-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Grouped Bar Chart"
]
},
{
"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.Bar(\n",
" x=['giraffes', 'orangutans', 'monkeys'],\n",
" y=[20, 14, 23],\n",
" name='SF Zoo'\n",
")\n",
"trace2 = go.Bar(\n",
" x=['giraffes', 'orangutans', 'monkeys'],\n",
" y=[12, 18, 29],\n",
" name='LA Zoo'\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"layout = go.Layout(\n",
" barmode='group'\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='grouped-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stacked Bar 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",
"trace1 = go.Bar(\n",
" x=['giraffes', 'orangutans', 'monkeys'],\n",
" y=[20, 14, 23],\n",
" name='SF Zoo'\n",
")\n",
"trace2 = go.Bar(\n",
" x=['giraffes', 'orangutans', 'monkeys'],\n",
" y=[12, 18, 29],\n",
" name='LA Zoo'\n",
")\n",
"\n",
"data = [trace1, trace2]\n",
"layout = go.Layout(\n",
" barmode='stack'\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='stacked-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bar Chart with Hover Text"
]
},
{
"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",
"trace0 = go.Bar(\n",
" x=['Product A', 'Product B', 'Product C'],\n",
" y=[20, 14, 23],\n",
" text=['27% market share', '24% market share', '19% market share'],\n",
" marker=dict(\n",
" color='rgb(158,202,225)',\n",
" line=dict(\n",
" color='rgb(8,48,107)',\n",
" width=1.5,\n",
" )\n",
" ),\n",
" opacity=0.6\n",
")\n",
"\n",
"data = [trace0]\n",
"layout = go.Layout(\n",
" title='January 2013 Sales Report',\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='text-hover-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bar Chart with Direct Labels"
]
},
{
"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",
"x = ['Product A', 'Product B', 'Product C']\n",
"y = [20, 14, 23]\n",
"\n",
"data = [go.Bar(\n",
" x=x,\n",
" y=y,\n",
" text=y,\n",
" textposition = 'auto',\n",
" marker=dict(\n",
" color='rgb(158,202,225)',\n",
" line=dict(\n",
" color='rgb(8,48,107)',\n",
" width=1.5),\n",
" ),\n",
" opacity=0.6\n",
" )]\n",
"\n",
"py.iplot(data, filename='bar-direct-labels')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Grouped Bar Chart with Direct Labels"
]
},
{
"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",
"x = ['Product A', 'Product B', 'Product C']\n",
"y = [20, 14, 23]\n",
"y2 = [16,12,27]\n",
"\n",
"trace1 = go.Bar(\n",
" x=x,\n",
" y=y,\n",
" text=y,\n",
" textposition = 'auto',\n",
" marker=dict(\n",
" color='rgb(158,202,225)',\n",
" line=dict(\n",
" color='rgb(8,48,107)',\n",
" width=1.5),\n",
" ),\n",
" opacity=0.6\n",
")\n",
"\n",
"trace2 = go.Bar(\n",
" x=x,\n",
" y=y2,\n",
" text=y2,\n",
" textposition = 'auto',\n",
" marker=dict(\n",
" color='rgb(58,200,225)',\n",
" line=dict(\n",
" color='rgb(8,48,107)',\n",
" width=1.5),\n",
" ),\n",
" opacity=0.6\n",
")\n",
"\n",
"data = [trace1,trace2]\n",
"\n",
"py.iplot(data, filename='grouped-bar-direct-labels')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rotated Bar Chart Labels"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Bar(\n",
" x=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',\n",
" 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],\n",
" y=[20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17],\n",
" name='Primary Product',\n",
" marker=dict(\n",
" color='rgb(49,130,189)'\n",
" )\n",
")\n",
"trace1 = go.Bar(\n",
" x=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',\n",
" 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],\n",
" y=[19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16],\n",
" name='Secondary Product',\n",
" marker=dict(\n",
" color='rgb(204,204,204)',\n",
" )\n",
")\n",
"\n",
"data = [trace0, trace1]\n",
"layout = go.Layout(\n",
" xaxis=dict(tickangle=-45),\n",
" barmode='group',\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='angled-text-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing Individual Bar Colors"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Bar(\n",
" x=['Feature A', 'Feature B', 'Feature C',\n",
" 'Feature D', 'Feature E'],\n",
" y=[20, 14, 23, 25, 22],\n",
" marker=dict(\n",
" color=['rgba(204,204,204,1)', 'rgba(222,45,38,0.8)',\n",
" 'rgba(204,204,204,1)', 'rgba(204,204,204,1)',\n",
" 'rgba(204,204,204,1)']),\n",
")\n",
"\n",
"data = [trace0]\n",
"layout = go.Layout(\n",
" title='Least Used Feature',\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='color-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing Individual Bar Widths"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace0 = go.Bar(\n",
" x=[1, 2, 3, 5.5, 10],\n",
" y=[10, 8, 6, 4, 2],\n",
" width = [0.8, 0.8, 0.8, 3.5, 4]\n",
")\n",
"\n",
"data = [trace0]\n",
"\n",
"fig = go.Figure(data=data)\n",
"py.iplot(fig, filename='width-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing Individual Bar Base"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"data = [\n",
" go.Bar(\n",
" x = ['2016','2017','2018'],\n",
" y = [500,600,700],\n",
" base = [-500,-600,-700],\n",
" marker = dict(\n",
" color = 'red'\n",
" ),\n",
" name = 'expenses'\n",
" ),\n",
" go.Bar(\n",
" x = ['2016','2017','2018'],\n",
" y = [300,400,700],\n",
" base = 0,\n",
" marker = dict(\n",
" color = 'blue'\n",
" ),\n",
" name = 'revenue'\n",
" )\n",
"]\n",
"\n",
"\n",
"fig = go.Figure(data=data)\n",
"py.iplot(fig, filename='base-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Colored and Styled Bar Chart"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace1 = go.Bar(\n",
" x=[1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,\n",
" 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012],\n",
" y=[219, 146, 112, 127, 124, 180, 236, 207, 236, 263,\n",
" 350, 430, 474, 526, 488, 537, 500, 439],\n",
" name='Rest of world',\n",
" marker=dict(\n",
" color='rgb(55, 83, 109)'\n",
" )\n",
")\n",
"trace2 = go.Bar(\n",
" x=[1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,\n",
" 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012],\n",
" y=[16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270,\n",
" 299, 340, 403, 549, 499],\n",
" name='China',\n",
" marker=dict(\n",
" color='rgb(26, 118, 255)'\n",
" )\n",
")\n",
"data = [trace1, trace2]\n",
"layout = go.Layout(\n",
" title='US Export of Plastic Scrap',\n",
" xaxis=dict(\n",
" tickfont=dict(\n",
" size=14,\n",
" color='rgb(107, 107, 107)'\n",
" )\n",
" ),\n",
" yaxis=dict(\n",
" title='USD (millions)',\n",
" titlefont=dict(\n",
" size=16,\n",
" color='rgb(107, 107, 107)'\n",
" ),\n",
" tickfont=dict(\n",
" size=14,\n",
" color='rgb(107, 107, 107)'\n",
" )\n",
" ),\n",
" legend=dict(\n",
" x=0,\n",
" y=1.0,\n",
" bgcolor='rgba(255, 255, 255, 0)',\n",
" bordercolor='rgba(255, 255, 255, 0)'\n",
" ),\n",
" barmode='group',\n",
" bargap=0.15,\n",
" bargroupgap=0.1\n",
")\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='style-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Waterfall Bar Chart"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"x_data = ['Product
Revenue', 'Services
Revenue',\n",
" 'Total
Revenue', 'Fixed
Costs',\n",
" 'Variable
Costs', 'Total
Costs', 'Total']\n",
"y_data = [400, 660, 660, 590, 400, 400, 340]\n",
"text = ['$430K', '$260K', '$690K', '$-120K', '$-200K', '$-320K', '$370K']\n",
"\n",
"# Base\n",
"trace0 = go.Bar(\n",
" x=x_data,\n",
" y=[0, 430, 0, 570, 370, 370, 0],\n",
" marker=dict(\n",
" color='rgba(1,1,1, 0.0)',\n",
" )\n",
")\n",
"# Revenue\n",
"trace1 = go.Bar(\n",
" x=x_data,\n",
" y=[430, 260, 690, 0, 0, 0, 0],\n",
" marker=dict(\n",
" color='rgba(55, 128, 191, 0.7)',\n",
" line=dict(\n",
" color='rgba(55, 128, 191, 1.0)',\n",
" width=2,\n",
" )\n",
" )\n",
")\n",
"# Costs\n",
"trace2 = go.Bar(\n",
" x=x_data,\n",
" y=[0, 0, 0, 120, 200, 320, 0],\n",
" marker=dict(\n",
" color='rgba(219, 64, 82, 0.7)',\n",
" line=dict(\n",
" color='rgba(219, 64, 82, 1.0)',\n",
" width=2,\n",
" )\n",
" )\n",
")\n",
"# Profit\n",
"trace3 = go.Bar(\n",
" x=x_data,\n",
" y=[0, 0, 0, 0, 0, 0, 370],\n",
" marker=dict(\n",
" color='rgba(50, 171, 96, 0.7)',\n",
" line=dict(\n",
" color='rgba(50, 171, 96, 1.0)',\n",
" width=2,\n",
" )\n",
" )\n",
")\n",
"data = [trace0, trace1, trace2, trace3]\n",
"layout = go.Layout(\n",
" title='Annual Profit- 2015',\n",
" barmode='stack',\n",
" paper_bgcolor='rgba(245, 246, 249, 1)',\n",
" plot_bgcolor='rgba(245, 246, 249, 1)',\n",
" showlegend=False\n",
")\n",
"\n",
"annotations = []\n",
"\n",
"for i in range(0, 7):\n",
" annotations.append(dict(x=x_data[i], y=y_data[i], text=text[i],\n",
" font=dict(family='Arial', size=14,\n",
" color='rgba(245, 246, 249, 1)'),\n",
" showarrow=False,))\n",
" layout['annotations'] = annotations\n",
"\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='waterfall-bar-profit')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bar Chart with Relative Barmode"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = [1, 2, 3, 4]\n",
"\n",
"trace1 = {\n",
" 'x': x,\n",
" 'y': [1, 4, 9, 16],\n",
" 'name': 'Trace1',\n",
" 'type': 'bar'\n",
"};\n",
"trace2 = {\n",
" 'x': x,\n",
" 'y': [6, -8, -4.5, 8],\n",
" 'name': 'Trace2',\n",
" 'type': 'bar'\n",
"};\n",
"trace3 = {\n",
" 'x': x,\n",
" 'y': [-15, -3, 4.5, -8],\n",
" 'name': 'Trace3',\n",
" 'type': 'bar'\n",
" }\n",
" \n",
"trace4 = {\n",
" 'x': x,\n",
" 'y': [-1, 3, -3, -4],\n",
" 'name': 'Trace4',\n",
" 'type': 'bar'\n",
" }\n",
" \n",
"data = [trace1, trace2, trace3, trace4];\n",
"layout = {\n",
" 'xaxis': {'title': 'X axis'},\n",
" 'yaxis': {'title': 'Y axis'},\n",
" 'barmode': 'relative',\n",
" 'title': 'Relative Barmode'\n",
"};\n",
"py.iplot({'data': data, 'layout': layout}, filename='barmode-relative')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Horizontal Bar Charts\n",
"See examples of horizontal bar charts [here](https://plotly.com/python/horizontal-bar-charts/)."
]
},
{
"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-barplot) can easily be deployed to a PaaS."
]
},
{
"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-barplot/\", width=\"100%\", height=\"650px\", frameBorder=\"0\")\n"
]
},
{
"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-barplot/code\", width=\"80%\", height=500, frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference\n",
"See https://plotly.com/python/reference/#bar for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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-uj6kwvwa\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-gf3f4eb5/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",
"import publisher\n",
"publisher.publish(\n",
" 'bars.ipynb', 'python/bar-charts/', 'Python Bar Charts | plotly',\n",
" 'How to make Bar Charts in Python with Plotly.',\n",
" title = 'Bar Charts | plotly',\n",
" name = 'Bar Charts',\n",
" thumbnail='thumbnail/bar.jpg', language='python',\n",
" page_type='example_index', has_thumbnail='true', display_as='basic', order=4,\n",
" ipynb= '~notebook_demo/186')"
]
}
],
"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": 1
}