Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1. loading presidential averages

538.com - actually http://fivethirtyeight.com - was a site that published data about the US presidential election
the site is no longer online, so we use a copy of its presidential averages that is stored in data/DATA.csv

load this file into a dataframe df; make sure to

2. plotting

And what we want to do is to plot the average of the polls for each candidate.
In other words, you should obtain something like this - we will arbitrarily focus on the 2024 year only

3. use interactivity

using the interactive view (if under Jupyter Lab) make sure to use %matplotlib ipympl, and zoom into the figure and retrieve

also write a line of code to compute this second date

4. race end

from this part we will focus on the period after first_harris_date

  1. how many candidates are still in the data ?

  2. make sure to keep only the 2 most famous ones

  3. how many data points do we have now for each of them ?

5: loading shapefiles with geopandas

we want to plot this data on a geographic map; first we need a definition of the various US states; this comes from the US Census Bureau (file us-states.zip), and we have a copy of it under data/

we load this shapefile into a geopandas object

  1. how many entries ?

  1. what are the various column types

as you can see, this is almost like a regular dataframe, except for the geometry column; which is a geographic entity, hence the term geo-dataframe

digression: using altair to produce a geographic viz

RendererRegistry.enable('html')

Here are a few examples of what we can do with this library

the simplest chart

Loading...

improving that view

now in terms of presentation, it is a little suboptimal, let’s improve this a bit

Loading...

using colors

now, the initial geo-dataframe has some numeric values, that we can use to color the map !
for example, there are AWATER and ALAND
which we can interpret as meaning area of water (as in lakes) and area of land respectively

Loading...

and we can use one of these to color the different states
for that we would use encode() like so

Loading...

better with a legend

Loading...

even better with tooltips

Loading...

our goal: geographic rendering

in this following we will produce a summary map, which looks like this:

6. first map

  1. for how many states do we have polls data ? which are they ?

  1. for this data, compute a table with the state as index, and the two contestants polls (averaged)

  1. using what you’ve seen in the digression, and what you will gather if needed on the Internet, produce the above map

7. outlining swing states

from the graph above, we want to outline the swing states
for example, we could come up with something like

8. filling gaps (optional)

this is not very readable; instead we want to still get the missing states show up in gray,
and to keep the outlining of the swing states, i.e.