Python, Jupyter Notebooks, and xarray introduction

During my dissertation research, I began playing with climate model output, which ended up integral in two of my three chapters. None of the work in the dissertation would have been possible without tools such as xarray which made working with climate model data as painless as can possibly be. I’m writing this blog post in 2022, and I originally wrote this script for a lab meeting in 2021 meant to demonstrate why Jupyter Notebooks are so great, however, I think the introduction to netCDFs (the default climate model format) and xarray are the real strengths of the code below. My lab at the time was invested in the R ecosystem, so the goal of the script below was to demonstrate how to leverage the strengths of two coding languages within the same script. If you follow along with the script below and have an introductory understanding of R and Python here’s what you’ll end up learning:

  • netCDF file structure
  • Opening and manipulating netCDF files in Python using xarray
  • Plot data from netCDF files in Python using matplotlib
  • Pass data from pandas in Python to R
  • Plot using ggplot2 in R

If following along with the script isn’t your cup of tea, feel free to open the script and actually run the code through Binder! Just click on the icon below and wait a few minutes for the Binder environment to build. From there, you’ll be able to run and edit the code for yourself in JupyterLab! If you’re unfamiliar with JupyterLab, wait for an upcoming blog post that introduces JupyterLab and how to use it for scientific computing - I’ll be sure to link it here once it’s up.

Click on this badge to run this code in the cloud:

Binder

David Fastovich
David Fastovich
Postdoctoral Scholar in Paleoclimate Dynamics

My research interests are focused on understanding past global change using proxies and models.