Skip to content

Instantly share code, notes, and snippets.

@nmathewa
Last active May 28, 2021 16:58
Show Gist options
  • Save nmathewa/89eca5f5352f47393cc32dc6c2822022 to your computer and use it in GitHub Desktop.
Save nmathewa/89eca5f5352f47393cc32dc6c2822022 to your computer and use it in GitHub Desktop.
cartopy spatial map netcdf4
"""
Created on Fri May 28 21:15:01 2021
@author: mathew
"""
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
dt = "ncfile.nc"# use your nc file
nc_data = xr.open_dataset(dt)
var = nc_data["ws10"][0,0,:,:]# use your variable and index
x = var.lon.values # longitude as x dimension
y = var.lat.values # latitude as y dimension
z = var.values # 2d array of spatial values
ax = plt.axes(projection=ccrs.PlateCarree())# Use the ccrs to set different projections
extent = [min(x),max(x),min(y),max(y)] # Extent of the plot from ncfile
ax.set_extent(extent)
ax.gridlines(draw_labels=True)
ax.coastlines(resolution='50m')
ax.add_feature(cartopy.feature.LAND) # not required will visualise small islands and other land forms(shapefile)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.RIVERS)
im = ax.pcolormesh(x,y,z)
plt.colorbar(im,location = 'bottom')
plt.title("10m wind speed over Indian Sub-Continent (m/s)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment