{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Two Scoreboards for Republican Presidential Candidates ## "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Starting with August 6, 2015, The New York Times updates from time to time a scoreboard for the republican presidential [candidates](http://www.nytimes.com/interactive/2015/08/06/upshot/2016-republican-presidential-candidates-dashboard.html).\n",
"\n",
"In this IPython (Jupyter) Notebook we generate the scoreboard published on August 14, respectively August 17, as [Heatmap(s)](https://plot.ly/python/heatmaps/) objects in Python Plotly."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inspecting the web page [source code](view-source:http://www.nytimes.com/interactive/2015/08/06/upshot/2016-republican-presidential-candidates-dashboard.html?abt=0002&abg=0)\n",
"we found out that the scoreboard heatmap in The New York Times is generated with [http://colorzilla.com/gradient-editor/]( http://colorzilla.com/gradient-editor/).\n",
"\n",
" \n",
"To identify the color code of each of the 16 colors defining the color gradient in The New York Times dashboard we install `ColorZilla`\n",
"[Chrome extension](https://chrome.google.com/webstore/detail/colorzilla/bhlhnicpbhignbdhedgjhgdocnmhomnp?hl=en).\n",
"\n",
"When the *newtimes* page is opened, we choose the *Web page color analyzer* in the `ColorZilla` menu and read\n",
"succesively the color codes."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"The corresponding [Plotly colorscale](https://plot.ly/python/heatmaps-contours-and-2dhistograms-tutorial/#Custom-color-scales-in-Plotly) is defined as follows:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"newyorktimes_cs=[[0.0, '#8B0000'],\n",
" [0.06666666666666667, '#9E051B'],\n",
" [0.13333333333333333, '#B0122C'],\n",
" [0.2, '#C0223B'],\n",
" [0.26666666666666666, '#CF3447'],\n",
" [0.3333333333333333, '#DB4551'],\n",
" [0.4, '#E75758'],\n",
" [0.4666666666666667, '#F06A5E'],\n",
" [0.5333333333333333, '#F87D64'],\n",
" [0.6, '#FE906A'],\n",
" [0.6666666666666666, '#FFA474'],\n",
" [0.7333333333333333, '#FFB880'],\n",
" [0.8, '#FFCB91'],\n",
" [0.8666666666666667, '#FFDEA7'],\n",
" [0.9333333333333333, '#FFEEC1'],\n",
" [1.0, '#FFFFE0']]\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we give the table of rankings as for 14 August, by the factors in the list with the same name."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"tab_vals14=[[1,2,3,4,5,6,6,8,9,9,9,12,13,13,13,13],\n",
" [1,7,5,12,5,4,12,7,2,3,12,7,7,12,7,12],\n",
" [4,7,2,1,10,5,6,7,9,12,3,14,12,11,15,16],\n",
" [2,9,4,1,3,8,10,11,6,5,6,14,14,12,14,13],\n",
" [1,3,4,14,8,2,13,12,7,6,9,16,5,10,12,15]]\n",
"\n",
"candidates=['Bush', 'Rubio', 'Walker', 'Trump', 'Kasich', 'Cruz', 'Fiorina', 'Huckabee', 'Paul']+\\\n",
" ['Christie', 'Carson', 'Santorum', 'Perry', 'Jindal', 'Graham', 'Pataki']\n",
"\n",
"factors=['Prediction Market', 'NationalEndorsements', 'Iowa Polls']+\\\n",
"['New Hampshire Polls', 'Money Raised']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we define a simple Plotly Heatmap:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import plotly.plotly as py \n",
"from plotly.graph_objs import *"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data14=Data([Heatmap(z=tab_vals14,\n",
" y=factors,\n",
" x=candidates,\n",
" colorscale=newyorktimes_cs,\n",
" showscale=False\n",
" )])"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"width = 900 \n",
"height =450\n",
"\n",
"anno_text=\"Data source:\\\n",
" [1]\"\n",
"\n",
"title = \"A scoreboard for republican candidates as of August 14, 2015\" \n",
"\n",
"layout = Layout(\n",
" title=' ', \n",
" font=Font(\n",
" family='Balto, sans-serif',\n",
" size=12,\n",
" color='rgb(68,68,68)'\n",
" ),\n",
" showlegend=False,\n",
" xaxis=XAxis(\n",
" title='', \n",
" showgrid=True,\n",
" side='top'\n",
" ),\n",
" yaxis=YAxis(\n",
" title='',\n",
" autorange='reversed',\n",
" showgrid=True, \n",
" autotick=False, \n",
" dtick=1 \n",
" ),\n",
" autosize=False, \n",
" height=height, \n",
" width=width,\n",
" margin=Margin(\n",
" l=135,\n",
" r=40,\n",
" b=85,\n",
" t=170\n",
" )\n",
")\n",
" \n",
"annotations = Annotations([\n",
" Annotation(\n",
" showarrow=False, \n",
" text=anno_text, \n",
" xref='paper', \n",
" yref='paper', \n",
" x=0, \n",
" y=-0.1, \n",
" xanchor='left', \n",
" yanchor='bottom', \n",
" font=Font(\n",
" size=11 )\n",
" )]) \n",
"\n",
"fig=Figure(data=data14, layout=layout) \n",
"fig['layout'].update(\n",
"title=title,\n",
"annotations=annotations\n",
") \n",
"\n",
"py.sign_in('empet', 'my_api_key')\n",
"py.iplot(fig,filename='Heatmap-republican-candidates-14')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we go further and update the above Figure with data available on August 17, and moreover we annotate the Heatmap, displaying the candidate ranking on each cell."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tab_vals17=[[1,2,3,4,5,6,7,7,9,9,11,11,13,13,13,13],\n",
" [1,7,5,12,5,4,7,12,2,12,3, 7,7,12,7,12],\n",
" [4,7,2,1,10,5,7, 6, 9,3, 12, 14,12,11,15,16],\n",
" [2,9,4,1,3,8,11, 10, 6,6, 5, 14,14,12,14,13],\n",
" [1,3,4,14,8,2,12, 13, 7,9, 6,16,5,10,11,15]]\n",
"\n",
"candidates17=['Bush', 'Rubio', 'Walker', 'Trump', 'Kasich', 'Cruz', 'Huckabee', 'Fiorina','Paul']+\\\n",
" ['Carson', 'Christie', 'Santorum', 'Perry', 'Jindal', 'Graham', 'Pataki']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first row in `tab_vals17` changed relative to the same row in `tab_vals14`, by swapping their positions the candidates (Fiorina, Huckabee) and (Christie, Carson), and correspondingly the other rows."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"fig['data'].update(Data([Heatmap(z=tab_vals17,\n",
" y=factors,\n",
" x=candidates17,\n",
" colorscale=newyorktimes_cs,\n",
" showscale=False\n",
" )]))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for i, row in enumerate(tab_vals17):\n",
" for j, val in enumerate(row):\n",
" annotations.append(\n",
" Annotation(\n",
" text=str(val), \n",
" x=candidates[j], y=factors[i],\n",
" xref='x1', yref='y1',\n",
" font=dict(color='white' if tab_vals17[i][j]<12 else 'rgb(150,150,150)'),\n",
" showarrow=False))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig['layout'].update(\n",
"title=\"A scoreboard for republican candidates as of August 17, 2015
Annotated heatmap\",\n",
"annotations=annotations\n",
") \n",
"\n",
"py.iplot(fig,filename='Annotated heatmap-republican-candidates-17')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.display import HTML\n",
"def css_styling():\n",
" styles = open(\"./custom.css\", \"r\").read()\n",
" return HTML(styles)\n",
"css_styling()"
]
}
],
"metadata": {
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}