Example: Plotting SAMPO L3 TROPOMI files¶
This simple Python code shows you how to plot a map of monthly mean NO2 concentrations over Eastern Africa using SAMPO L3 data. First import the necessary packages:
In [1]:
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
Next, load your NetCDF file that you have previously downloaded from the SAMPO service:
In [2]:
data = xr.open_dataset('./S5P_OFFL_L3_NO2_monthlycomposite_202311.nc')
Access the data using its variable name
In [3]:
no2_data = data['tropospheric_NO2_column_number_density'][0,:,:]
Adjust the longitude for proper plotting and create a meshgrid
In [4]:
longitude = data['longitude'].values
longitude[longitude > 180] -= 360
latitude = data['latitude'].values
lon, lat = np.meshgrid(longitude, latitude)
Since latitude and longitude are now 2D arrays from the meshgrid, we need a different approach to apply the mask. We will apply the mask directly when plotting, by setting the extent and limits. We are now ready to plot the map:
In [5]:
# Create a plot with Cartopy
fig = plt.figure(figsize=(10, 12))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent([20, 50, -20, 20], crs=ccrs.PlateCarree()) # Set the extent to focus on Europe
# Add features
ax.add_feature(cfeature.BORDERS, linestyle=':') # Add country borders
ax.add_feature(cfeature.COASTLINE) # Add coastlines
# Plot the NO2 data
no2_data_plot = ax.pcolormesh(lon, lat, no2_data, cmap='viridis', shading='auto', vmin=0, vmax=5, transform=ccrs.PlateCarree())
# Add a colorbar
cbar = plt.colorbar(no2_data_plot, orientation='horizontal', pad=0.02, aspect=16, shrink=0.8)
cbar.set_label('Tropospheric NO$_2$ column number density (Pmolec./cm$^2$)')
plt.title('Mean NO$_2$ distribution over Eastern Africa - November 2023')
plt.show()