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’)

 

[pastacode lang=”python” manual=”%23%20import%20packages%0Aimport%20pandas%20as%20pd%0Aimport%20matplotlib.patches%20as%20mpatches%0Aimport%20matplotlib.pyplot%20as%20plt%0Aimport%20numpy%20as%20np%0Afrom%20scipy.ndimage.filters%20import%20gaussian_filter1d%0A%0A%23%20read%20in%20csv%0Adf%20%3D%20pd.read_csv(‘%2Fpath%2Fto%2Fyour%2Fcsv%2Ffile.csv’%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20index_col%3D0%2Cparse_dates%3DTrue)%0A%0A%23%20Create%20lists%20for%20countries%2C%20database%20columns%2C%20colors%0Aw_africa%20%3D%20(‘SEN’%2C’GMB’%2C’GNB’%2C’GIN’%2C’SLE’%2C’LBR’)%0Aw_df%20%3D%20(df.SEN%2C%20df.GMB%2C%20df.GNB%2C%20df.GIN%2C%20df.SLE%2C%20df.LBR)%0Acolors%20%3D%20(‘%23FF0000’%2C’%23FF3300’%2C’%23FF6600’%2C’%23FF9900’%2C’%23FFCC00’%2C’%23FFFF00’)%0A%0A%23%20wrap%20plotting%20inside%20style%20context%20for%20nice%20looking%20plot%20theme%0Awith%20plt.style.context(‘Solarize_Light2′)%3A%0A%20%20%20%20%0A%20%20%20%20%23%20smooth%20plots%20with%20gaussian%20filter%2C%20set%20sigma%20for%20amount%20of%20smoothing%0A%20%20%20%20for%20(w%2C%20wd%2C%20c)%20in%20zip(w_africa%2C%20w_df%2C%20colors)%3A%20%0A%20%20%20%20%20%20%20%20s%20%3D%20gaussian_filter1d(wd%2C%20sigma%3D5)%0A%20%20%20%20%20%20%20%20plt.plot(df.index%2C%20s%20%2Clinestyle%3D’–‘%2Ccolor%3Dc)%20%0A%20%20%20%20%0A%20%20%20%20%23%20setup%20plot%20with%20labels%20and%20title%0A%20%20%20%20fig%20%3D%20plt.gcf()%0A%20%20%20%20fig.set_size_inches(18.5%2C%2010.5)%0A%20%20%20%20plt.xlabel(‘Year’%2C%20fontsize%3D14)%0A%20%20%20%20plt.ylabel(‘Celcius’%2C%20fontsize%3D14)%0A%20%20%20%20fig.suptitle(‘Area%20averaged%20ERA5%20temperature%20data%2C%20for%20selected%20countries%20in%20W.Africa.’%2C%20fontsize%3D16)%0A%20%20%20%20%0A%20%20%20%20%23%20add%20the%20unsmoothed%20line%20plots%20per%20country%20for%20reference%0A%20%20%20%20for%20wd%2C%20c%20in%20zip(w_df%2C%20colors)%3A%0A%20%20%20%20%20%20%20%20wd.plot(color%20%3D%20c)%0A%20%20%20%20plt.legend()%0A%0A%23%20display%20plot%0Aplt.show()” message=”” highlight=”” provider=”manual”/]

Download the source csv file here

View code on Github here

Luke Sanger – WEMC Data Engineer, July 2019

Recent Posts