Last active
May 28, 2021 16:58
-
-
Save nmathewa/89eca5f5352f47393cc32dc6c2822022 to your computer and use it in GitHub Desktop.
cartopy spatial map netcdf4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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