Skip to content

Instantly share code, notes, and snippets.

@suntong
Last active December 14, 2015 17:34
Show Gist options
  • Save suntong/6e3f4cbd8056c699c0d2 to your computer and use it in GitHub Desktop.
Save suntong/6e3f4cbd8056c699c0d2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotly, Grouped Bar Chart\n",
"\n",
"Ref:\n",
"\n",
"**Tables With Graphs** \n",
"https://plot.ly/python/table/\n",
"\n",
"\n",
"**BAR CHART WITH LINE PLOT** \n",
"https://plot.ly/python/horizontal-bar-charts/#bar-chart-with-line-plot"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"# Add graph data\n",
"teams = ['Montréal Canadiens', 'Dallas Stars', 'NY Rangers',\n",
"'Boston Bruins', 'Chicago Blackhawks', 'LA Kings', 'Ottawa Senators']\n",
"GFPG = [3.54, 3.48, 3.0, 3.27, 2.83, 2.45, 3.18]\n",
"GAPG = [2.17, 2.57, 2.0, 2.91, 2.57, 2.14, 2.77]\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~xpt/85.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Make traces for graph\n",
"trace1 = go.Bar(x=teams, y=GFPG, type='bar',\n",
" marker=dict(color='#0099ff'),\n",
" name='Goals For<br>Per Game',\n",
" xaxis='x2', yaxis='y2')\n",
"trace2 = go.Bar(x=teams, y=GAPG, type='bar',\n",
" marker=dict(color='#404040'),\n",
" name='Goals Against<br>Per Game',\n",
" xaxis='x2', yaxis='y2')\n",
"\n",
"# Add trace data to figure\n",
"data = [trace1, trace2]\n",
"\n",
"# Plot!\n",
"fig = go.Figure(data=data) #, layout=layout)\n",
"py.iplot(fig, filename='tutorial/bar_plotly_grouped')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~xpt/88.embed\" height=\"525px\" width=\"100%\"></iframe>"
],
"text/plain": [
"<plotly.tools.PlotlyDisplay object>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Make traces for graph\n",
"trace1 = go.Bar(y=teams, x=GFPG, type='bar', orientation = 'h',\n",
" marker=dict(color='#0099ff'),\n",
" name='Goals For<br>Per Game',\n",
" xaxis='x2', yaxis='y2')\n",
"trace2 = go.Bar(y=teams, x=GAPG, type='bar', orientation = 'h',\n",
" marker=dict(color='#404040'),\n",
" name='Goals Against<br>Per Game',\n",
" xaxis='x2', yaxis='y2')\n",
"\n",
"# Add trace data to figure\n",
"data = [trace1, trace2]\n",
"\n",
"layout = go.Layout(\n",
" yaxis=dict(\n",
" autorange='reversed'\n",
" )#, \n",
"# margin=Margin(\n",
"# l=160,\n",
"# pad=5\n",
"# )\n",
")\n",
"\n",
"# Plot!\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='tutorial/bar_plotly_grouped_h')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"annotations = []\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='x', yref='y', 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",
"\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",
"plot_url = py.plot(fig, filename='oecd-networth-saving-bar-line')"
]
}
],
"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.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment