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 plt.style.context(‘Solarize_Light2’)

Then each line is given a hex value to precisely define the color of the plot line, this value is matched in the mpatches

First load python packages and csv file:

import pandas as pd
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
 
df = pd.read_csv('path/to/csv/file.csv', 
                 index_col=0,parse_dates=True)

 

Matplotlib plotting:

### WITH SMOOTHING APPLIED ###

from scipy.ndimage.filters import gaussian_filter1d
with plt.style.context('Solarize_Light2'):

    #senegal
    SEN_patch = mpatches.Patch(color='#FF0000', label='Senegal')
    SENsmoothed = gaussian_filter1d(df.SEN, sigma=5)
    plt.plot(df.index, SENsmoothed,linestyle='--',color='#FF0000')  
    
    #gambia
    GMB_patch = mpatches.Patch(color='#FF3300', label='Gambia')
    GMBsmoothed = gaussian_filter1d(df.GMB, sigma=5)
    plt.plot(df.index, GMBsmoothed,linestyle='--',color='#FF3300')      
    
    #guinea-bissau
    GNB_patch = mpatches.Patch(color='#FF6600', label='Guinea-bissau')
    GNBsmoothed = gaussian_filter1d(df.GNB, sigma=5)
    plt.plot(df.index, GNBsmoothed,linestyle='--',color='#FF6600')   
    
    #guinea
    GIN_patch = mpatches.Patch(color='#FF9900', label='Guinea')
    GINsmoothed = gaussian_filter1d(df.GIN, sigma=5)
    plt.plot(df.index, GINsmoothed,linestyle='--',color='#FF9900')  
    
    #sierra-leone
    SLE_patch = mpatches.Patch(color='#FFCC00', label='Sierra-Leone')
    SLEsmoothed = gaussian_filter1d(df.SLE, sigma=5)
    plt.plot(df.index, SLEsmoothed,linestyle='--',color='#FF9900')  

    #liberia
    LBR_patch = mpatches.Patch(color='#FFFF00', label='Liberia')
    LBRsmoothed = gaussian_filter1d(df.LBR, sigma=5)
    plt.plot(df.index, LBRsmoothed,linestyle='--', color='#FFFF00')
    
    fig = plt.gcf()
    fig.set_size_inches(18.5, 10.5)
    #fig.savefig('test2png.png', dpi=100)
    
    
    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)
    
    #line plots per country
    df.SEN.plot(color='#FF0000')
    df.GMB.plot(color='#FF3300')
    df.GNB.plot(color='#FF6600')
    df.GIN.plot(color='#FF9900')
    df.SLE.plot(color='#FFCC00')
    df.LBR.plot(color='#FFFF00')
    plt.legend(handles=[SEN_patch,GMB_patch,GNB_patch,GIN_patch,SLE_patch,LBR_patch])
    
plt.show()
Recent Posts

Start typing and press Enter to search