Skip to content

Instantly share code, notes, and snippets.

@willyhagi
Created February 28, 2019 15:47
Show Gist options
  • Save willyhagi/f574fcfd7cd51585d704d69929597eea to your computer and use it in GitHub Desktop.
Save willyhagi/f574fcfd7cd51585d704d69929597eea to your computer and use it in GitHub Desktop.

Climate Heatmaps Done Easy

Investigating NCEI's Paleoclimatology Database with Pandas and Seaborn

The Concept of Climate Stripes

Some time ago Dr. Ed Hawkins, who happens to be the creator of the Climate Spirals, released to the world the Warming Stripes graph for Annual Global Temperature ranging from 1850-2017. The concept is simple but also very informative: each stripe represents the temperature for a single year and as the time series goes on the changes get to be very visible for anyone to see.

Essentially the stripes together make a Heatmap, and Heatmaps are easy to make with a little help from Seaborn. The purpose here is to expand the Warming Stripes for a couple more centuries to look the temperature changes from a slightly different perspective, but for that you need data.

Climate for Cavemen

Paleoclimatology, as the name suggests, is the kind of study focused on the conditions of distant era's climate -- from a few centuries ago to the first thousand years since our planet came to existence. 

You might be wandering: how can we know? It's a little complicated, but paleoclimatology thrives by using data known as proxies, indirect measures of temperature and rainfall contained within tree rings, ice cores, corals and other natural sources which are carefully analyzed to provide good reconstructions of climate information.

There are many places you can go to download paleoclimatological time series, but your first stop should probably be NOAA's National Centers for Environmental Information (NCEI) database. For scientists and data scientists with a taste for climate, this is a gold mine (but is not the only one).

Putting It All Together

The data you'll use represents a time series reconstruction of Northern Hemisphere's Temperature from 1000-1998 shown in the seminal work of Mann, Bradley and Hughes (1999). You might know this from the Hockey Stick graph, but now you'll get the opportunity to make your own version of the graph here.

To start things up, import the usual suspects:

https://gist.github.com/c1bfeec201f098fe7b719a3d6befeece

Reading Files

With Pandas you can read the text file containing the time series with read_csv():

https://gist.github.com/b8c31b87a477c3ebb4fc4350c398fc97

The delim_whitespace=True argument recognizes data columns separated by a white space and with header=None you say to Pandas your data has no header and no particular name for your columns, so the time series within the data file look like this:

https://gist.github.com/95da57f0be8ba38b15871aace9c0b9eb

The columns are recognized by an index number, with '0' for the first column (the year column) and '1' the second (the temperature data). Good news is that Pandas allows you to rename the columns with any str name you prefer, such as:

https://gist.github.com/eb4c32139ba98d7ddc776ec6ba4031af

Your Own Hockey Stick

One amazing Seaborn feature is the possibility to set style and context for your graphs according to different tastes, like:

https://gist.github.com/ece073b10cb7ace65d29a516ffadb256

While there are many options the style selected is the ticks and the context talk, defining also the lines.linewidth from the Matplotlib parameters. Check the Seaborn documentation for more.

With the style ready, it's time to plot your Hockey Stick graph with a combination of Seaborn and Matplotlib:

https://gist.github.com/61acdaf6e9a8f604c3d8d83e8a539b17

Place hockey stick graph here

What's happening up there? With sns.lineplot() you plotted the Temperature data pretty much like you'd do with the standard plt.plot() from Matplotlib while the red and blue shaded regions represent (with a poetic license) the Medieval Warm Period and the Little Ice Age with the help from axvspan(). The set_xlabel() and set_ylabel() functions are familiar with anyone with a contact with Matplotlib, but here you saw they also work well with LaTeX for the $\degree$ symbol.

But enough of Hockey, where are the Stripes?

Paleoclimate Stripes

Looking at the original Warming Stripes as a Heatmap the graph doesn't show any color bar nor axis labels and ticks, so it's important to supress these to keep the fidelity of your own stripes. For that, Seaborn provides lots of options enough to make a good approximation.

One thing to pay attention is the fact that Seaborn requires a 2D array as input data, while your time series is a simple 1D array:

https://gist.github.com/7105ef2d634a9616855ec4eea98509c8

One way to get around this problem is to use NumPy and make a new dimension with np.newaxis. With this solved it's easy to make your own Stripes:

https://gist.github.com/9895a6682599944d17c00f61099df151

Place Warming Stripes Here

Using sns.heatmap() your arguments were:

  • data: the mbh99 temperature time series
  • cmap and cbar: the seismic colormap from the Matplotlib Library (other cool ones are: RdYlBu_r, seismic, coolwarm, bwr, RdBu_r) and False to supress the colorbar
  • vmin, vmax and center: the minimum (-.4) and maximum (.1) limits to plot the colors, while the red and blue diverge at 0.
  • xticklabels and yticklabels: all False to make just the stripes visible

There you are, your own Warming Stripes. Quite easy, isn't it?

Conclusion

Paleoclimatology is the study of distant time's climate, from a few centuries ago to the first days of Earth. NCEI's Paleoclimate Database allows anyone to download several datasets from different studies, such as Mann, Bradley and Hughes (1999) but you can get data from many others as well.

With a little of Pandas and Seaborn you can read data and make Heatmaps for any time series you are interested, such as the one you used here, and make your own version of the Warming Stripes (plus many other stuff as well). Have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment