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.

presidential averages

538.com - actually it is http://fivethirtyeight.com - is a site hosted by ABC news, that exposes data about the US presidential election

we’re gonna use the data in this URL:

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

using the interactive view (after all we are using %matplotlib ipympl), zoom into the figure and retrieve

also write a line of code to compute this second date

race end

from this part we will focus on the period after first_harris_date

how many candidates are still in the data ?
make sure to keep only the 2 most famous ones

geographic rendering

in this section we will produce a summary map, which looks like this
the color depicts the ratio between, otoh Harris’s average score over time, and otoh Trump’s
also the tooltips allow to expose more details on the individual results

first we need a definition of the various US states; there is one here

and we can load it like so:

Loading...

so 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 2: using altair to produce a geographic visualization

of course you might have to install altair... how do you go about doing that again ?

RendererRegistry.enable('html')
Loading...
Loading...

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

Loading...

now, the initial geo-dataframe has some numeric values, that we can use to color the map !

for example, there are AWATER and ALAND - that I take it mean area of water and area of land respectively
and we can use one of these to color the different states

for that we just do, like for simpler altair plots we call encode() like so

Loading...
Loading...
Loading...

back to our data

given this knowledge, you should be able to produce our target graph, namely again

focusing on swing states

from the graph above, keep only the following states