Debian Codegraph @

bqplot/examples/Applications/Visualizing the US Elections.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "## Visualizing the 2016 General Election Polls"
8 ]
9 },
10 {
11 "cell_type": "code",
12 "execution_count": null,
13 "metadata": {
14 "collapsed": true
15 },
16 "outputs": [],
17 "source": [
18 "from __future__ import print_function\n",
19 "import pandas as pd\n",
20 "import numpy as np\n",
21 "from ipywidgets import VBox, HBox\n",
22 "import os\n",
23 "\n",
24 "codes = pd.read_csv(os.path.abspath('../data_files/state_codes.csv'))"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": null,
30 "metadata": {
31 "collapsed": true
32 },
33 "outputs": [],
34 "source": [
35 "try:\n",
36 " from pollster import Pollster\n",
37 "except ImportError:\n",
38 " print('Pollster not found. Installing Pollster..')\n",
39 " try:\n",
40 " import subprocess\n",
41 " subprocess.check_call(['pip', 'install', 'pollster==0.1.6'])\n",
42 " except:\n",
43 " print(\"The pip installation failed. Please manually install Pollster and re-run this notebook.\")"
44 ]
45 },
46 {
47 "cell_type": "code",
48 "execution_count": null,
49 "metadata": {
50 "collapsed": true
51 },
52 "outputs": [],
53 "source": [
54 "def get_candidate_data(question):\n",
55 " clinton, trump, undecided, other = 0., 0., 0., 0.\n",
56 " for candidate in question['subpopulations'][0]['responses']:\n",
57 " if candidate['last_name'] == 'Clinton':\n",
58 " clinton = candidate['value']\n",
59 " elif candidate['last_name'] == 'Trump':\n",
60 " trump = candidate['value']\n",
61 " elif candidate['choice'] == 'Undecided':\n",
62 " undecided = candidate['value']\n",
63 " else:\n",
64 " other = candidate['value']\n",
65 " return clinton, trump, other, undecided\n",
66 "\n",
67 "def get_row(question, partisan='Nonpartisan', end_date='2016-06-21'):\n",
68 " # if question['topic'] != '2016-president':\n",
69 " if ('2016' in question['topic']) and ('Presidential' in question['topic']):\n",
70 " hillary, donald, other, undecided = get_candidate_data(question)\n",
71 " return [{'Name': question['name'], 'Partisan': partisan, 'State': question['state'],\n",
72 " 'Date': np.datetime64(end_date), 'Trump': donald, 'Clinton': hillary, 'Other': other,\n",
73 " 'Undecided': undecided}]\n",
74 " else:\n",
75 " return\n",
76 "\n",
77 "def analyze_polls(polls):\n",
78 " global data\n",
79 " for poll in polls:\n",
80 " for question in poll.questions:\n",
81 " resp = get_row(question, partisan=poll.partisan, end_date=poll.end_date)\n",
82 " if resp is not None:\n",
83 " data = data.append(resp)\n",
84 " return"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": null,
90 "metadata": {
91 "collapsed": true
92 },
93 "outputs": [],
94 "source": [
95 "try:\n",
96 " from pollster import Pollster\n",
97 " pollster = Pollster()\n",
98 " \n",
99 " # Getting data from Pollster. This might take a second.\n",
100 " raw_data = pollster.charts(topic='2016-president')\n",
101 " \n",
102 " data = pd.DataFrame(columns=['Name', 'Partisan', 'State', 'Date', 'Trump', 'Clinton', 'Other',\n",
103 " 'Undecided'])\n",
104 " \n",
105 " for i in raw_data:\n",
106 " analyze_polls(i.polls())\n",
107 "except:\n",
108 " raise ValueError('Please install Pollster and run the functions above')"
109 ]
110 },
111 {
112 "cell_type": "code",
113 "execution_count": null,
114 "metadata": {
115 "collapsed": true
116 },
117 "outputs": [],
118 "source": [
119 "def get_state_party(code):\n",
120 " state = codes[codes['FIPS']==code]['USPS'].values[0]\n",
121 " if data[data['State']==state].shape[0] == 0:\n",
122 " return None\n",
123 " polls = data[(data['State']==state) & (data['Trump'] > 0.) & (data['Clinton'] > 0.)].sort_values(by='Date')\n",
124 " if polls.shape[0] == 0:\n",
125 " return None\n",
126 " if (polls.tail(1)['Trump'] > polls.tail(1)['Clinton']).values[0]:\n",
127 " return 'Republican'\n",
128 " else:\n",
129 " return 'Democrat'\n",
130 "\n",
131 "def get_color_data():\n",
132 " color_data = {}\n",
133 " for i in codes['FIPS']:\n",
134 " color_data[i] = get_state_party(i)\n",
135 " return color_data\n",
136 "\n",
137 "def get_state_data(code):\n",
138 " state = codes[codes['FIPS']==code]['USPS'].values[0]\n",
139 " if data[data['State']==state].shape[0] == 0:\n",
140 " return None\n",
141 " polls = data[(data['State']==state) & (data['Trump'] > 0.) & (data['Clinton'] > 0.)].sort_values(by='Date')\n",
142 " return polls"
143 ]
144 },
145 {
146 "cell_type": "code",
147 "execution_count": null,
148 "metadata": {
149 "collapsed": true
150 },
151 "outputs": [],
152 "source": [
153 "from bqplot import *\n",
154 "from ipywidgets import Layout"
155 ]
156 },
157 {
158 "cell_type": "code",
159 "execution_count": null,
160 "metadata": {
161 "collapsed": true
162 },
163 "outputs": [],
164 "source": [
165 "dt_x = DateScale()\n",
166 "sc_y = LinearScale()\n",
167 "\n",
168 "time_series = Lines(scales={'x': dt_x, 'y': sc_y}, colors=['#E91D0E', '#2aa1ec'], marker='circle')\n",
169 "\n",
170 "ax_x = Axis(scale=dt_x, label='Date')\n",
171 "ax_y = Axis(scale=sc_y, orientation='vertical', label='Percentage')\n",
172 "\n",
173 "ts_fig = Figure(marks=[time_series], axes=[ax_x, ax_y], title='General Election - State Polls', \n",
174 " layout=Layout(min_width='650px', min_height='400px'))"
175 ]
176 },
177 {
178 "cell_type": "code",
179 "execution_count": null,
180 "metadata": {
181 "collapsed": true
182 },
183 "outputs": [],
184 "source": [
185 "sc_geo = AlbersUSA()\n",
186 "sc_c1 = OrdinalColorScale(domain=['Democrat', 'Republican'], colors=['#2aa1ec', '#E91D0E'])\n",
187 "\n",
188 "color_data = get_color_data()\n",
189 "\n",
190 "map_styles = {'color': color_data,\n",
191 " 'scales': {'projection': sc_geo, 'color': sc_c1}, 'colors': {'default_color': 'Grey'}}\n",
192 "\n",
193 "axis = ColorAxis(scale=sc_c1)\n",
194 "\n",
195 "states_map = Map(map_data=topo_load('map_data/USStatesMap.json'), tooltip=ts_fig, **map_styles)\n",
196 "map_fig = Figure(marks=[states_map], axes=[axis],title='General Election Polls - State Wise')"
197 ]
198 },
199 {
200 "cell_type": "code",
201 "execution_count": null,
202 "metadata": {
203 "collapsed": true
204 },
205 "outputs": [],
206 "source": [
207 "def hover_callback(name, value):\n",
208 " polls = get_state_data(value['data']['id'])\n",
209 " if polls is None or polls.shape[0] == 0:\n",
210 " time_series.y = [0.]\n",
211 " return\n",
212 " time_series.x, time_series.y = polls['Date'].values.astype(np.datetime64), [polls['Trump'].values, polls['Clinton'].values]\n",
213 " ts_fig.title = str(codes[codes['FIPS']==value['data']['id']]['Name'].values[0]) + ' Polls - Presidential Election'"
214 ]
215 },
216 {
217 "cell_type": "code",
218 "execution_count": null,
219 "metadata": {
220 "collapsed": true
221 },
222 "outputs": [],
223 "source": [
224 "states_map.on_hover(hover_callback)"
225 ]
226 },
227 {
228 "cell_type": "code",
229 "execution_count": null,
230 "metadata": {
231 "collapsed": true,
232 "scrolled": false
233 },
234 "outputs": [],
235 "source": [
236 "national = data[(data['State']=='US') & (data['Trump'] > 0.) & (data['Clinton'] > 0.)].sort_values(by='Date')\n",
237 "\n",
238 "dt_x = DateScale()\n",
239 "sc_y = LinearScale()\n",
240 "\n",
241 "clinton_scatter = Scatter(x=national['Date'].values.astype(np.datetime64), y=national['Clinton'],\n",
242 " scales={'x': dt_x, 'y': sc_y}, \n",
243 " colors=['#2aa1ec'])\n",
244 "\n",
245 "trump_scatter = Scatter(x=national['Date'].values.astype(np.datetime64), y=national['Trump'],\n",
246 " scales={'x': dt_x, 'y': sc_y},\n",
247 " colors=['#E91D0E'])\n",
248 "\n",
249 "ax_x = Axis(scale=dt_x, label='Date', tick_format='%b-%Y', num_ticks=8)\n",
250 "ax_y = Axis(scale=sc_y, orientation='vertical', label='Percentage')\n",
251 "\n",
252 "scat_fig = Figure(marks=[clinton_scatter, trump_scatter], axes=[ax_x, ax_y], title='General Election - National Polls')"
253 ]
254 },
255 {
256 "cell_type": "markdown",
257 "metadata": {},
258 "source": [
259 "#### Hover on the map to visualize the poll data for that state."
260 ]
261 },
262 {
263 "cell_type": "code",
264 "execution_count": null,
265 "metadata": {
266 "scrolled": true
267 },
268 "outputs": [],
269 "source": [
270 "VBox([map_fig, scat_fig])"
271 ]
272 },
273 {
274 "cell_type": "markdown",
275 "metadata": {},
276 "source": [
277 "## Visualizing the County Results of the 2008 Elections"
278 ]
279 },
280 {
281 "cell_type": "code",
282 "execution_count": null,
283 "metadata": {
284 "collapsed": true
285 },
286 "outputs": [],
287 "source": [
288 "county_data = pd.read_csv(os.path.abspath('../data_files/2008-election-results.csv'))"
289 ]
290 },
291 {
292 "cell_type": "code",
293 "execution_count": null,
294 "metadata": {
295 "collapsed": true
296 },
297 "outputs": [],
298 "source": [
299 "winner = np.array(['McCain'] * county_data.shape[0])"
300 ]
301 },
302 {
303 "cell_type": "code",
304 "execution_count": null,
305 "metadata": {
306 "collapsed": true
307 },
308 "outputs": [],
309 "source": [
310 "winner[(county_data['Obama'] > county_data['McCain']).values] = 'Obama'"
311 ]
312 },
313 {
314 "cell_type": "code",
315 "execution_count": null,
316 "metadata": {
317 "collapsed": true,
318 "scrolled": true
319 },
320 "outputs": [],
321 "source": [
322 "sc_geo_county = AlbersUSA()\n",
323 "sc_c1_county = OrdinalColorScale(domain=['McCain', 'Obama'], colors=['Red', 'DeepSkyBlue'])\n",
324 "\n",
325 "color_data_county = dict(zip(county_data['FIPS'].values.astype(int), list(winner)))\n",
326 "\n",
327 "map_styles_county = {'color': color_data_county,\n",
328 " 'scales': {'projection': sc_geo_county, 'color': sc_c1_county}, 'colors': {'default_color': 'Grey'}}\n",
329 "\n",
330 "axis_county = ColorAxis(scale=sc_c1_county)\n",
331 "\n",
332 "county_map = Map(map_data=topo_load('map_data/USCountiesMap.json'), **map_styles_county)\n",
333 "county_fig = Figure(marks=[county_map], axes=[axis_county],title='US Elections 2008 - Example',\n",
334 " layout=Layout(min_width='800px', min_height='550px'))"
335 ]
336 },
337 {
338 "cell_type": "code",
339 "execution_count": null,
340 "metadata": {
341 "collapsed": true
342 },
343 "outputs": [],
344 "source": [
345 "names_sc = OrdinalScale(domain=['Obama', 'McCain'])\n",
346 "vote_sc_y = LinearScale(min=0, max=100.)\n",
347 "\n",
348 "names_ax = Axis(scale=names_sc, label='Candidate')\n",
349 "vote_ax = Axis(scale=vote_sc_y, orientation='vertical', label='Percentage')\n",
350 "\n",
351 "vote_bars = Bars(scales={'x': names_sc, 'y': vote_sc_y}, colors=['#2aa1ec', '#E91D0E'])\n",
352 "\n",
353 "bar_fig = Figure(marks=[vote_bars], axes=[names_ax, vote_ax], title='Vote Margin',\n",
354 " layout=Layout(min_width='600px', min_height='400px'))"
355 ]
356 },
357 {
358 "cell_type": "code",
359 "execution_count": null,
360 "metadata": {
361 "collapsed": true
362 },
363 "outputs": [],
364 "source": [
365 "def county_hover(name, value):\n",
366 " if (county_data['FIPS'] == value['data']['id']).sum() == 0:\n",
367 " bar_fig.title = ''\n",
368 " vote_bars.y = [0., 0.]\n",
369 " return\n",
370 " votes = county_data[county_data['FIPS'] == value['data']['id']]\n",
371 " dem_vote = float(votes['Obama %'].values[0])\n",
372 " rep_vote = float(votes['McCain %'].values[0])\n",
373 " vote_bars.x, vote_bars.y = ['Obama', 'McCain'], [dem_vote, rep_vote]\n",
374 " bar_fig.title = 'Vote % - ' + value['data']['name']\n",
375 " \n",
376 "county_map.on_hover(county_hover)\n",
377 "county_map.tooltip = bar_fig"
378 ]
379 },
380 {
381 "cell_type": "markdown",
382 "metadata": {},
383 "source": [
384 "#### Hover on the map to visualize the voting percentage for each candidate in that county"
385 ]
386 },
387 {
388 "cell_type": "code",
389 "execution_count": null,
390 "metadata": {
391 "scrolled": true
392 },
393 "outputs": [],
394 "source": [
395 "county_fig"
396 ]
397 }
398 ],
399 "metadata": {
400 "kernelspec": {
401 "display_name": "Python 3",
402 "language": "python",
403 "name": "python3"
404 },
405 "language_info": {
406 "codemirror_mode": {
407 "name": "ipython",
408 "version": 3
409 },
410 "file_extension": ".py",
411 "mimetype": "text/x-python",
412 "name": "python",
413 "nbconvert_exporter": "python",
414 "pygments_lexer": "ipython3",
415 "version": "3.7.0"
416 }
417 },
418 "nbformat": 4,
419 "nbformat_minor": 1
420 }