Visualizing Climate Change Data with Python

Creating climate change graphs with the xarray and cartopy Python libraries

Giannis Tolios
Towards Data Science

--

Global Temperature Anomaly for 2020 (image by author) — Data Provided By NASA/GISS

It is an undeniable fact that climate change poses the biggest challenge for humanity in the current era. The global mean temperature is constantly rising and the IPCC has pointed out the increase should be limited to 1.5 °C above pre-industrial levels, to have any hope of mitigating the harmful effects of climate change. This goal was set in the Paris Agreement that was adopted by almost every country in the world on 2015. Unfortunately though, little action is being taken to deal with the problem, highlighted by the alarming scientific research that is constantly published.

The COVID-19 pandemic led to a drop in most aspects of human activity, and the subsequent decrease of fossil fuel burning and CO2 emissions. Regardless of that, 2020 was one of the hottest years in history, with extreme weather events being recorded all over the globe. Furthermore, according to research published in the Cryosphere journal, ice sheets are melting at record rates across the planet, being in line with the worst-case scenarios of IPCC¹. Other scientists claim that climate change and biodiversity loss pose an unprecedented existential threat for humanity, on a scale that is difficult to grasp even for experts².

According to surveys conducted by the Pew Research Center, about 70% of people worldwide believe that climate change is a major threat to their country. This percentage has increased significantly compared to 2013, but it is still low, as everyone must understand the magnitude of this problem. One way to inform the public about climate change is by creating informative and aesthetically appealing visualizations of the associated data. In this article, I am going to teach you how to create map charts and animations of temperature variability, by using Python.

The GISTEMP Dataset

Our visualizations are based on the NASA GISTEMP v4 dataset that combines NOAA GHCN v4 (meteorological stations) and ERSST v5 (ocean areas), thus resulting into a comprehensive record of climate variability for the entire surface of our planet. The dataset includes temperatures from 1880–present on a monthly frequency, and is used by NASA to monitor global and regional climate variability. Instead of providing absolute temperatures, the anomaly compared to a base period (1951–1980) is given instead. The dataset is freely available at the GISTEMP website in netCDF format. In case you want to read more details about the dataset and the ways NASA analyzes it to generate graphs about climate change, you can check out the associated paper³.

The Xarray and Cartopy Libraries

NetCDF is a file format that supports array-oriented scientific data, and is commonly used in fields such as climatology and meteorology. There are various Python libraries that support it, but we are going to use xarray because it is one of the most powerful. Xarray is inspired by the popular pandas library, extending much of its functionality to multi-dimensional arrays. The library is built on top of NumPy, making it easy to integrate in a Python machine learning workflow. Finally, it also has powerful plotting capabilities for the visualization of scientific datasets. Cartopy is a Python library used for geospatial data visualization that enables scientists to create publication-quality maps. It integrates well with xarray, so we are going to generate our maps with it.

Visualizing the GISTEMP Data

The following code can be executed on a Jupyter notebook using Python 3, or even Google Colab if you prefer. First of all, we are going to import the necessary libraries and load the GISTEMP dataset, by using the open_dataset() xarray function.

(image by author)

Printing the xarray dataset outputs some basic information about its attributes and variables. As we can see, the dataset is a 2°×2° grid of temperature anomalies, on a monthly frequency. We are now going to plot a global map of the temperature anomaly for the year 2020.

Global Temperature Anomaly in 2020 (image by author)

First of all, we resample the xarray dataset to a yearly frequency, as it is more convenient to work with. After doing that, we create a cartopy map and add the boundaries of every country in the world. Then, we plot the xarray data by using the imshow() method. We also enabled bicubic interpolation, so the graph looks more visually appealing. We are now going to shift our focus to european countries, by plotting the map of that region.

Europe Temperature Anomaly in 2020 (image by author)

We changed the extents of the cartopy GeoAxes to plot the region of our preference only, instead of the entire map. In this case, we plotted the european continent, but you can choose any region you prefer, depending on the audience you wish to share the graph with. We are now going to create an animation of temperature variability in the past decades for the same region.

Instead of generating a static chart, we now used the FuncAnimation() Matplotlib function to generate an animation for the 1950–2020 period. An additional method named animate() is used, specifying how each frame of the animation will be generated. This function is passed as an argument to FuncAnimation(), along with the time period we want to be animated. After doing that, we save the animation as an MP4 video, by using FFmpeg. Download links and instructions for installing FFmpeg on all major operating systems are available here.

Conclusion

Creating informative and appealing charts for climate change is a relatively easy process, and I hope this tutorial will help you accomplish that. The scientific datasets that are provided freely by reputable organizations such as NASA, as well as the numerous open source tools, make climate data analysis accessible to everyone. I encourage you to work with GISTEMP or other similar datasets, and inform the citizens of your region about climate change. We can all contribute in dealing with that enormous challenge, and utilizing data is a great way to achieve that. In case you want to clone the Github repository of this project, it is available here. Feel free to share your thoughts in the comments, or follow me on LinkedIn where I regularly post content about data science, climate change and other topics. You can also visit my personal website or check my latest book, titled Simplifying Machine Learning with PyCaret.

References

[1] Slater, Thomas, et al. “Earth’s ice imbalance.” The Cryosphere 15.1 (2021): 233–246.

[2] Bradshaw, Corey JA, et al. “Underestimating the challenges of avoiding a ghastly future.” Frontiers in Conservation Science 1 (2021): 9.

[3] Hansen, James, et al. “Global surface temperature change.” Reviews of Geophysics 48.4 (2010).

--

--