Tech Blog 6: Using Python and Matplotlib for plotting ERA5 data

A quick/dirty tech blog today, getting to know some of Matplotlib’s extra features for generating some attractive plots!

Using yearly ERA5 temperature data from 1979 to present obtained from the Copernicus Climate Data Store,

the data was masked by country using shapefiles from Natural Earth and then an average was taken for the area (see previous blogs on area averaging for information on how to do this).

This was plotted to view whether there was any notable trends. As expected, all countries appear to be increasing in temperature over time. When the plots are smoothed using a gaussian_filter, the rise in temperature shows a clear trend (highlighted with dotted plot).

The plotting theme is achieved by declaring the matplotlib code inside‘Solarize_Light2’)


# import packages
import pandas as pd
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter1d

# read in csv
df = pd.read_csv('/path/to/your/csv/file.csv', 

# Create lists for countries, database columns, colors
w_africa = ('SEN','GMB','GNB','GIN','SLE','LBR')
w_df = (df.SEN, df.GMB, df.GNB, df.GIN, df.SLE, df.LBR)
colors = ('#FF0000','#FF3300','#FF6600','#FF9900','#FFCC00','#FFFF00')

# wrap plotting inside style context for nice looking plot theme
    # smooth plots with gaussian filter, set sigma for amount of smoothing
    for (w, wd, c) in zip(w_africa, w_df, colors): 
        s = gaussian_filter1d(wd, sigma=5)
        plt.plot(df.index, s ,linestyle='--',color=c) 
    # setup plot with labels and title
    fig = plt.gcf()
    fig.set_size_inches(18.5, 10.5)
    plt.xlabel('Year', fontsize=14)
    plt.ylabel('Celcius', fontsize=14)
    fig.suptitle('Area averaged ERA5 temperature data, for selected countries in W.Africa.', fontsize=16)
    # add the unsmoothed line plots per country for reference
    for wd, c in zip(w_df, colors):
        wd.plot(color = c)

# display plot

Download the source csv file here

View code on Github here

Luke Sanger – WEMC Data Engineer, July 2019

Recent Posts