{
"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!\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.0.6'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Basic Horizontal 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=[20, 14, 23],\n",
" y=['giraffes', 'orangutans', 'monkeys'],\n",
" orientation = 'h'\n",
")]\n",
"\n",
"py.iplot(data, filename='horizontal-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Colored Horizontal 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",
"trace1 = go.Bar(\n",
" y=['giraffes', 'orangutans', 'monkeys'],\n",
" x=[20, 14, 23],\n",
" name='SF Zoo',\n",
" orientation = 'h',\n",
" marker = dict(\n",
" color = 'rgba(246, 78, 139, 0.6)',\n",
" line = dict(\n",
" color = 'rgba(246, 78, 139, 1.0)',\n",
" width = 3)\n",
" )\n",
")\n",
"trace2 = go.Bar(\n",
" y=['giraffes', 'orangutans', 'monkeys'],\n",
" x=[12, 18, 29],\n",
" name='LA Zoo',\n",
" orientation = 'h',\n",
" marker = dict(\n",
" color = 'rgba(58, 71, 80, 0.6)',\n",
" line = dict(\n",
" color = 'rgba(58, 71, 80, 1.0)',\n",
" width = 3)\n",
" )\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='marker-h-bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Color Palette for Bar Chart"
]
},
{
"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",
"top_labels = ['Strongly
agree', 'Agree', 'Neutral', 'Disagree',\n",
" 'Strongly
disagree']\n",
"\n",
"colors = ['rgba(38, 24, 74, 0.8)', 'rgba(71, 58, 131, 0.8)',\n",
" 'rgba(122, 120, 168, 0.8)', 'rgba(164, 163, 204, 0.85)',\n",
" 'rgba(190, 192, 213, 1)']\n",
"\n",
"x_data = [[21, 30, 21, 16, 12],\n",
" [24, 31, 19, 15, 11],\n",
" [27, 26, 23, 11, 13],\n",
" [29, 24, 15, 18, 14]]\n",
"\n",
"y_data = ['The course was effectively
organized',\n",
" 'The course developed my
abilities and skills ' +\n",
" 'for
the subject', 'The course developed ' +\n",
" 'my
ability to think critically about
the subject',\n",
" 'I would recommend this
course to a friend']\n",
"\n",
"\n",
"traces = []\n",
"\n",
"for i in range(0, len(x_data[0])):\n",
" for xd, yd in zip(x_data, y_data):\n",
" traces.append(go.Bar(\n",
" x=[xd[i]],\n",
" y=[yd],\n",
" orientation='h',\n",
" marker=dict(\n",
" color=colors[i],\n",
" line=dict(\n",
" color='rgb(248, 248, 249)',\n",
" width=1)\n",
" )\n",
" ))\n",
"\n",
"layout = go.Layout(\n",
" xaxis=dict(\n",
" showgrid=False,\n",
" showline=False,\n",
" showticklabels=False,\n",
" zeroline=False,\n",
" domain=[0.15, 1]\n",
" ),\n",
" yaxis=dict(\n",
" showgrid=False,\n",
" showline=False,\n",
" showticklabels=False,\n",
" zeroline=False,\n",
" ),\n",
" barmode='stack',\n",
" paper_bgcolor='rgb(248, 248, 255)',\n",
" plot_bgcolor='rgb(248, 248, 255)',\n",
" margin=dict(\n",
" l=120,\n",
" r=10,\n",
" t=140,\n",
" b=80\n",
" ),\n",
" showlegend=False,\n",
")\n",
"\n",
"annotations = []\n",
"\n",
"for yd, xd in zip(y_data, x_data):\n",
" # labeling the y-axis\n",
" annotations.append(dict(xref='paper', yref='y',\n",
" x=0.14, y=yd,\n",
" xanchor='right',\n",
" text=str(yd),\n",
" font=dict(family='Arial', size=14,\n",
" color='rgb(67, 67, 67)'),\n",
" showarrow=False, align='right'))\n",
" # labeling the first percentage of each bar (x_axis)\n",
" annotations.append(dict(xref='x', yref='y',\n",
" x=xd[0] / 2, y=yd,\n",
" text=str(xd[0]) + '%',\n",
" font=dict(family='Arial', size=14,\n",
" color='rgb(248, 248, 255)'),\n",
" showarrow=False))\n",
" # labeling the first Likert scale (on the top)\n",
" if yd == y_data[-1]:\n",
" annotations.append(dict(xref='x', yref='paper',\n",
" x=xd[0] / 2, y=1.1,\n",
" text=top_labels[0],\n",
" font=dict(family='Arial', size=14,\n",
" color='rgb(67, 67, 67)'),\n",
" showarrow=False))\n",
" space = xd[0]\n",
" for i in range(1, len(xd)):\n",
" # labeling the rest of percentages for each bar (x_axis)\n",
" annotations.append(dict(xref='x', yref='y',\n",
" x=space + (xd[i]/2), y=yd, \n",
" text=str(xd[i]) + '%',\n",
" font=dict(family='Arial', size=14,\n",
" color='rgb(248, 248, 255)'),\n",
" showarrow=False))\n",
" # labeling the Likert scale\n",
" if yd == y_data[-1]:\n",
" annotations.append(dict(xref='x', yref='paper',\n",
" x=space + (xd[i]/2), y=1.1,\n",
" text=top_labels[i],\n",
" font=dict(family='Arial', size=14,\n",
" color='rgb(67, 67, 67)'),\n",
" showarrow=False))\n",
" space += xd[i]\n",
"\n",
"layout['annotations'] = annotations\n",
"\n",
"fig = go.Figure(data=traces, layout=layout)\n",
"py.iplot(fig, filename='bar-colorscale')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bar Chart with Line Plot"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the format of your plot grid:\n",
"[ (1,1) x1,y1 ] [ (1,2) x2,y2 ]\n",
"\n"
]
},
{
"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",
"from plotly import tools\n",
"\n",
"import numpy as np\n",
"\n",
"y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,\n",
" 7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998\n",
" ]\n",
"y_net_worth = [93453.919999999998, 81666.570000000007, 69889.619999999995,\n",
" 78381.529999999999, 141395.29999999999, 92969.020000000004,\n",
" 66090.179999999993, 122379.3]\n",
"x_saving = ['Japan', 'United Kingdom', 'Canada', 'Netherlands',\n",
" 'United States', 'Belgium', 'Sweden', 'Switzerland']\n",
"x_net_worth = ['Japan', 'United Kingdom', 'Canada', 'Netherlands',\n",
" 'United States', 'Belgium', 'Sweden', 'Switzerland'\n",
" ]\n",
"trace0 = go.Bar(\n",
" x=y_saving,\n",
" y=x_saving,\n",
" marker=dict(\n",
" color='rgba(50, 171, 96, 0.6)',\n",
" line=dict(\n",
" color='rgba(50, 171, 96, 1.0)',\n",
" width=1),\n",
" ),\n",
" name='Household savings, percentage of household disposable income',\n",
" orientation='h',\n",
")\n",
"trace1 = go.Scatter(\n",
" x=y_net_worth,\n",
" y=x_net_worth,\n",
" mode='lines+markers',\n",
" line=dict(\n",
" color='rgb(128, 0, 128)'),\n",
" name='Household net worth, Million USD/capita',\n",
")\n",
"layout = dict(\n",
" title='Household savings & net worth for eight OECD countries',\n",
" yaxis=dict(\n",
" showgrid=False,\n",
" showline=False,\n",
" showticklabels=True,\n",
" domain=[0, 0.85],\n",
" ),\n",
" yaxis2=dict(\n",
" showgrid=False,\n",
" showline=True,\n",
" showticklabels=False,\n",
" linecolor='rgba(102, 102, 102, 0.8)',\n",
" linewidth=2,\n",
" domain=[0, 0.85],\n",
" ),\n",
" xaxis=dict(\n",
" zeroline=False,\n",
" showline=False,\n",
" showticklabels=True,\n",
" showgrid=True,\n",
" domain=[0, 0.42],\n",
" ),\n",
" xaxis2=dict(\n",
" zeroline=False,\n",
" showline=False,\n",
" showticklabels=True,\n",
" showgrid=True,\n",
" domain=[0.47, 1],\n",
" side='top',\n",
" dtick=25000,\n",
" ),\n",
" legend=dict(\n",
" x=0.029,\n",
" y=1.038,\n",
" font=dict(\n",
" size=10,\n",
" ),\n",
" ),\n",
" margin=dict(\n",
" l=100,\n",
" r=20,\n",
" t=70,\n",
" b=70,\n",
" ),\n",
" paper_bgcolor='rgb(248, 248, 255)',\n",
" plot_bgcolor='rgb(248, 248, 255)',\n",
")\n",
"\n",
"annotations = []\n",
"\n",
"y_s = np.round(y_saving, decimals=2)\n",
"y_nw = np.rint(y_net_worth)\n",
"\n",
"# Adding labels\n",
"for ydn, yd, xd in zip(y_nw, y_s, x_saving):\n",
" # labeling the scatter savings\n",
" annotations.append(dict(xref='x2', yref='y2',\n",
" y=xd, x=ydn - 20000,\n",
" text='{:,}'.format(ydn) + 'M',\n",
" font=dict(family='Arial', size=12,\n",
" color='rgb(128, 0, 128)'),\n",
" showarrow=False))\n",
" # labeling the bar net worth\n",
" annotations.append(dict(xref='x1', yref='y1',\n",
" y=xd, x=yd + 3,\n",
" text=str(yd) + '%',\n",
" font=dict(family='Arial', size=12,\n",
" color='rgb(50, 171, 96)'),\n",
" showarrow=False))\n",
"# Source\n",
"annotations.append(dict(xref='paper', yref='paper',\n",
" x=-0.2, y=-0.109,\n",
" text='OECD \"' +\n",
" '(2015), Household savings (indicator), ' +\n",
" 'Household net worth (indicator). doi: ' +\n",
" '10.1787/cfc6f499-en (Accessed on 05 June 2015)',\n",
" font=dict(family='Arial', size=10,\n",
" color='rgb(150,150,150)'),\n",
" showarrow=False))\n",
"\n",
"layout['annotations'] = annotations\n",
"\n",
"# Creating two subplots\n",
"fig = tools.make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,\n",
" shared_yaxes=False, vertical_spacing=0.001)\n",
"\n",
"fig.append_trace(trace0, 1, 1)\n",
"fig.append_trace(trace1, 1, 2)\n",
"\n",
"fig['layout'].update(layout)\n",
"py.iplot(fig, filename='oecd-networth-saving-bar-line')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reference\n",
"See more examples of bar charts and styling options [here](https://plot.ly/python/bar-charts/).
See https://plot.ly/python/reference/#bar 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": "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",
"import publisher\n",
"publisher.publish(\n",
" 'horizontal-bars.ipynb', 'python/horizontal-bar-charts/', 'Horizontal Bar Charts | plotly',\n",
" 'How to make horizontal bar charts in Python with Plotly.',\n",
" title = 'Horizontal Bar Charts | plotly',\n",
" name = 'Horizontal Bar Charts',\n",
" thumbnail='thumbnail/horizontal-bar.jpg', language='python', \n",
" has_thumbnail='true', display_as='basic', order=5,\n",
" ipynb= '~notebook_demo/5')"
]
},
{
"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
}