Skip to content

Instantly share code, notes, and snippets.

@msund
Last active February 15, 2023 14:07
  • Star 0 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save msund/96bd1d837f4139b2558d to your computer and use it in GitHub Desktop.
Tableau with Python
{
"metadata": {
"name": "",
"signature": "sha256:173944e2e44f322cb8868203194673670f98a52ac4bbebf75c09a46a03ba149d"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Plotting With Python,\n",
"Tableau, & Plotly"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import plotly.plotly as py\n",
"from plotly.graph_objs import *\n",
"import plotly.tools as tls\n",
"import pandas as pd"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import plotly\n",
"plotly.__version__"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"'1.6.14'"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"py.sign_in('Python-Demo-Account','gwt101uhh0')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The graph below was built by [Andy Kriebel](https://twitter.com/VizWizBI). Here, we're embedding the Workbook inside our IPython Notebook. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<script type='text/javascript' src='https://public.tableau.com/javascripts/api/viz_v1.js'></script><div class='tableauPlaceholder' style='width: 754px; height: 619px;'><noscript><a href='http:&#47;&#47;vizwiz.blogspot.com&#47;2015&#47;03&#47;makeover-monday-there-are-only-three.html'><img alt='Woman Power ' src='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Wo&#47;WomenManagers&#47;WomanPower&#47;1_rss.png' style='border: none' /></a></noscript><object class='tableauViz' width='754' height='619' style='display:none;'><param name='host_url' value='https%3A%2F%2Fpublic.tableau.com%2F' /> <param name='site_root' value='' /><param name='name' value='WomenManagers&#47;WomanPower' /><param name='tabs' value='no' /><param name='toolbar' value='yes' /><param name='static_image' value='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Wo&#47;WomenManagers&#47;WomanPower&#47;1.png' /> <param name='animate_transition' value='yes' /><param name='display_static_image' value='yes' /><param name='display_spinner' value='yes' /><param name='display_overlay' value='yes' /><param name='display_count' value='yes' /><param name='showVizHome' value='no' /><param name='showTabs' value='y' /></object></div>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df = pd.read_csv(\"http://public.tableausoftware.com/views/WomenManagers/WomanPower.csv\")"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df.head() # take a look at data"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Country</th>\n",
" <th>Latitude (generated)</th>\n",
" <th>Longitude (generated)</th>\n",
" <th>% of women managers</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td> Albania</td>\n",
" <td> 40.6540</td>\n",
" <td> 20.076</td>\n",
" <td> 22.5%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td> Algeria</td>\n",
" <td> 28.6045</td>\n",
" <td> 2.640</td>\n",
" <td> 4.9%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td> Argentina</td>\n",
" <td>-33.1660</td>\n",
" <td> -64.310</td>\n",
" <td> 31.0%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td> Aruba</td>\n",
" <td> 12.5560</td>\n",
" <td> -70.024</td>\n",
" <td> 41.0%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td> Australia</td>\n",
" <td>-24.5780</td>\n",
" <td> 133.582</td>\n",
" <td> 36.2%</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
" Country Latitude (generated) Longitude (generated) % of women managers\n",
"0 Albania 40.6540 20.076 22.5%\n",
"1 Algeria 28.6045 2.640 4.9%\n",
"2 Argentina -33.1660 -64.310 31.0%\n",
"3 Aruba 12.5560 -70.024 41.0%\n",
"4 Australia -24.5780 133.582 36.2%"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can plot the data inside Plotly and make a histogram."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"histogram_plot = [{'x': df['% of women managers'], \n",
" 'type': 'histogram',\n",
" 'name': '% of women managers',\n",
"}]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data_histogram = Data(histogram_plot)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig_histogram = Figure(data=data_histogram)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"py.iplot(fig_histogram, filename='% of women managers')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~Python-Demo-Account/1547.embed\" height=\"525\" width=\"100%\"></iframe>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"<plotly.tools.PlotlyDisplay at 0x7fe619b82c10>"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"tls.embed('MattSundquist',8651) # style in GUI"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~MattSundquist/8651.embed\" height=\"525\" width=\"100%\"></iframe>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"<plotly.tools.PlotlyDisplay at 0x7fe619b82810>"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig = py.get_figure('MattSundquist',8651) # or, I can get the data"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fig_data = fig.get_data()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"str(fig_data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"\"[[u'22.5%', u'4.9%', u'31.0%', u'41.0%', u'36.2%', u'30.0%', u'34.2%', u'44.4%', u'5.4%', u'43.4%', u'46.2%', u'32.4%', u'41.3%', u'43.5%', u'27.6%', u'35.1%', u'38.6%', u'37.3%', u'36.4%', u'18.0%', u'36.2%', u'42.4%', u'23.6%', u'16.8%', u'53.1%', u'27.9%', u'27.6%', u'38.1%', u'15.8%', u'26.2%', u'28.4%', u'38.6%', u'39.7%', u'9.7%', u'37.1%', u'32.8%', u'22.1%', u'29.7%', u'39.4%', u'34.0%', u'31.1%', u'39.0%', u'25.1%', u'44.8%', u'23.9%', u'33.2%', u'38.6%', u'39.9%', u'21.2%', u'14.6%', u'32.6%', u'31.8%', u'25.8%', u'59.3%', u'11.1%', u'5.1%', u'37.2%', u'36.4%', u'14.8%', u'13.9%', u'32.3%', u'45.7%', u'8.4%', u'21.4%', u'38.8%', u'18.0%', u'31.4%', u'23.1%', u'36.6%', u'21.5%', u'13.4%', u'27.0%', u'23.4%', u'32.0%', u'44.1%', u'41.9%', u'27.3%', u'12.8%', u'36.0%', u'29.0%', u'40.0%', u'41.0%', u'32.2%', u'9.3%', u'3.0%', u'14.4%', u'47.4%', u'32.3%', u'29.3%', u'47.6%', u'37.8%', u'34.6%', u'6.8%', u'31.4%', u'39.1%', u'34.0%', u'52.3%', u'7.1%', u'29.1%', u'40.8%', u'31.4%', u'33.1%', u'39.0%', u'31.3%', u'11.0%', u'30.0%', u'28.4%', u'35.5%', u'33.2%', u'10.2%', u'16.5%', u'28.2%', u'43.1%', u'14.8%', u'12.2%', u'20.2%', u'39.9%', u'10.0%', u'34.2%', u'42.7%', u'43.9%', u'28.5%', u'33.4%', u'23.0%', u'2.1%', u'20.6%']]\""
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If someone runs this Notebook, they will fetch the most recent, up to date data from Tableau. If you are using updating graphs in Tableau, that gives you a quick way to grab and share data and graphs between everyone. Similarly, if you embed a live-streaming or 3D in a Tableau dashboard, as we've done below, you can also see live updating data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<script type='text/javascript' src='https://public.tableau.com/javascripts/api/viz_v1.js'></script><div class='tableauPlaceholder' style='width: 804px; height: 1669px;'><noscript><a href='#'><img alt='TalkingTurkey ' src='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Pl&#47;Plotly_3D_Streamin_0&#47;TalkingTurkey&#47;1_rss.png' style='border: none' /></a></noscript><object class='tableauViz' width='804' height='1669' style='display:none;'><param name='host_url' value='https%3A%2F%2Fpublic.tableau.com%2F' /> <param name='site_root' value='' /><param name='name' value='Plotly_3D_Streamin_0&#47;TalkingTurkey' /><param name='tabs' value='no' /><param name='toolbar' value='yes' /><param name='static_image' value='https:&#47;&#47;public.tableau.com&#47;static&#47;images&#47;Pl&#47;Plotly_3D_Streamin_0&#47;TalkingTurkey&#47;1.png' /> <param name='animate_transition' value='yes' /><param name='display_static_image' value='yes' /><param name='display_spinner' value='yes' /><param name='display_overlay' value='yes' /><param name='display_count' value='yes' /><param name='showVizHome' value='no' /><param name='showTabs' value='y' /></object></div>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import HTML\n",
"import urllib2\n",
"HTML(urllib2.urlopen('https://raw.githubusercontent.com/plotly/python-user-guide/css-updates/custom.css').read())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<style>\n",
"/*\n",
"Placeholder for custom user CSS\n",
"\n",
"mainly to be overridden in profile/static/custom/custom.css\n",
"\n",
"This will always be an empty file in IPython\n",
"*/\n",
"\n",
"\n",
"div.input_area {\n",
" border: 0;\n",
" background: rgb(244, 244, 248);\n",
"}\n",
"\n",
".cm-s-ipython span.cm-keyword {\n",
" color: #447bdc;\n",
"}\n",
"\n",
"h1 {\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
"}\n",
".text_cell_render h1 {\n",
" font-weight: 300;\n",
" font-size: 65px;\n",
" line-height: 66px;\n",
" letter-spacing: -0.25px;\n",
" color: #0089fe;\n",
" margin-bottom: 0em;\n",
" margin-top: 0em;\n",
" display: block;\n",
" white-space: nowrap;\n",
" margin: 0;\n",
"}\n",
"h2 {\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
"}\n",
".text_cell_render h2 {\n",
" font-weight: 300;\n",
" font-size: 48px;\n",
" line-height: 49px;\n",
" color:#0089fe;\n",
" display: block;\n",
" white-space: nowrap;\n",
" margin: 0;\n",
"}\n",
"h3 {\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
"}\n",
".text_cell_render h3 {\n",
" font-weight: 300;\n",
" font-size: 36px;\n",
" line-height: 37px;\n",
" color:#0089fe;\n",
" display: block;\n",
" white-space: nowrap;\n",
" margin: 0;\n",
"}\n",
"h4 {\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
"}\n",
".text_cell_render h4 {\n",
" font-weight: 300;\n",
" font-size: 21px;\n",
" line-height: 22px;\n",
" color:#0089fe;\n",
" display: block;\n",
" white-space: nowrap;\n",
" margin: 0;\n",
"}\n",
"\n",
"h5 {\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
"}\n",
".text_cell_render h5 {\n",
" font-weight: 300;\n",
" font-size: 28px;\n",
" line-height: 35px;\n",
" color:#1e3a7b;\n",
" display: block;\n",
" white-space: nowrap;\n",
" font-style: normal;\n",
" margin: 0;\n",
"}\n",
"\n",
"h6 {\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
"}\n",
".text_cell_render h6 {\n",
" font-weight: 300;\n",
" font-size: 20px;\n",
" line-height: 25px;\n",
" color:#1e3a7b;\n",
" display: block;\n",
" white-space: nowrap;\n",
" font-style: normal;\n",
"}\n",
"\n",
"div.text_cell_render{\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
" line-height: 110%;\n",
" text-align:justify;\n",
" text-justify:inter-word;\n",
" color: #1e4a7b;\n",
"}\n",
"div.text_cell_render p{\n",
" font-weight: 300;\n",
" font-size: 18px;\n",
" line-height: 25px;\n",
" color: #1e3a7b;\n",
"}\n",
"div.output_subarea.output_text.output_pyout {\n",
" overflow-x: auto;\n",
" overflow-y: scroll;\n",
" max-height: 300px;\n",
"}\n",
"div.output_subarea.output_stream.output_stdout.output_text {\n",
" overflow-x: auto;\n",
" overflow-y: scroll;\n",
" max-height: 300px;\n",
"}\n",
"div.output_subarea.output_html.rendered_html {\n",
" overflow-x: scroll;\n",
" max-width: 100%;\n",
" /* overflow-y: scroll; */\n",
" /* max-height: 300px; */\n",
"}\n",
"code{\n",
" font-size: 78%;\n",
"}\n",
".rendered_html code{\n",
" background-color: transparent;\n",
" white-space: inherit;\n",
"}\n",
"ul{\n",
" /* color:#265cc3; */\n",
" margin: 2em;\n",
"}\n",
"ul li{\n",
" padding-left: 0.5em;\n",
" margin-bottom: 0.5em;\n",
" margin-top: 0.5em;\n",
"}\n",
"ul li li{\n",
" padding-left: 0.2em;\n",
" margin-bottom: 0.2em;\n",
" margin-top: 0.2em;\n",
"}\n",
"ol{\n",
" /* color:#265cc3; */\n",
" margin: 2em;\n",
"}\n",
"ol li{\n",
" padding-left: 0.5em;\n",
" margin-bottom: 0.5em;\n",
" margin-top: 0.5em;\n",
"}\n",
"/*.prompt{\n",
" display: None;\n",
"} */\n",
"ul li{\n",
" padding-left: 0.5em;\n",
" margin-bottom: 0.5em;\n",
" margin-top: 0.2em;\n",
"}\n",
"a:link{\n",
" font-weight: bold;\n",
" color:#265cc3;\n",
"}\n",
"a:visited{\n",
" font-weight: bold;\n",
" color: #1d3b84;\n",
"}\n",
"a:hover{\n",
" font-weight: bold;\n",
" color: #1d3b84;\n",
"}\n",
"a:focus{\n",
" font-weight: bold;\n",
" color:#265cc3;\n",
"}\n",
"a:active{\n",
" font-weight: bold;\n",
" color:#265cc3;\n",
"}\n",
".rendered_html :link {\n",
" text-decoration: none;\n",
"}\n",
".rendered_html :hover {\n",
" text-decoration: none;\n",
"}\n",
".rendered_html :visited {\n",
" text-decoration: none;\n",
"}\n",
".rendered_html :focus {\n",
" text-decoration: none;\n",
"}\n",
".rendered_html :active {\n",
" text-decoration: none;\n",
"}\n",
".warning{\n",
" color: rgb( 240, 20, 20 )\n",
"}\n",
"hr {\n",
" color: #f3f3f3;\n",
" background-color: #f3f3f3;\n",
" height: 1px;\n",
"}\n",
"blockquote{\n",
" display:block;\n",
" background: #f3f3f3;\n",
" font-family: \"Open sans\",verdana,arial,sans-serif;\n",
" width:610px;\n",
" padding: 15px 15px 15px 15px;\n",
" text-align:justify;\n",
" text-justify:inter-word;\n",
" }\n",
" blockquote p {\n",
" margin-bottom: 0;\n",
" line-height: 125%;\n",
" font-size: 100%;\n",
" text-align: center;\n",
" }\n",
"/* element.style {\n",
"} */\n",
"</script>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"<IPython.core.display.HTML at 0x7fe619b82950>"
]
}
],
"prompt_number": 14
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment