steve
steve

Reputation: 531

Python netcdf cartopy - Plotting a selection of data

I have a netcdf file ('test.nc'). The variables of the netcdf file are the following:

variables(dimensions): float64 lon(lon), float64 lat(lat), int32 crs(), int16 Band1(lat,lon)

I am interested in the ´Band1´ variable. Using cartopy, I could plot the data using the following code:

import numpy as np
import pandas as pd
import gzip
from netCDF4 import Dataset,num2date
import time
import matplotlib.pyplot as plt
import os 
import matplotlib as mplt
#mplt.use('Agg')
import cartopy.crs as ccrs
import cartopy.feature as cfea
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

projection=ccrs.PlateCarree()
bbox=[-180,180,-60,85];creg='glob'
mplt.rc('xtick', labelsize=9) 
mplt.rc('ytick', labelsize=9)

nc = Dataset('test.nc','r')
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
kopi= (nc.variables['Band1'][:,:])
nc.close()

fig=plt.figure(figsize=(11,5))
ax=fig.add_subplot(1,1,1,projection=projection)
ax.set_extent(bbox,projection)
ax.add_feature(cfea.COASTLINE,lw=.5)
ax.add_feature(cfea.RIVERS,lw=.5)
ax.add_feature(cfea.BORDERS, linewidth=0.6, edgecolor='dimgray')
ax.background_patch.set_facecolor('.9')
levels=[1,4,8,11,14,17,21,25,29]
cmap=plt.cm.BrBG
norm=mplt.colors.BoundaryNorm(levels,cmap.N)
ddlalo=.25
pc=ax.contourf(lon,lat,kopi,levels=levels,transform=projection,cmap=cmap,norm=norm,extend='both')
divider = make_axes_locatable(ax)
ax_cb = divider.new_horizontal(size="3%", pad=0.1, axes_class=plt.Axes)
fig.colorbar(pc,extend='both', cax=ax_cb)
fig.add_axes(ax_cb)
fig.colorbar(pc,extend='both', cax=ax_cb)
ttitle='Jony'
ax.set_title(ttitle,loc='left',fontsize=9)
plt.show()

enter image description here

However, I would like just to plot a selection of values inside the variable ´Band1´. I thought I could use the following code:

kopi= (nc.variables['Band1'][:,:])<=3

However it does not work and instead of plotting the area corresponding to the value selection it selected the all map. enter image description here

How could I select and plot a desired range of values inside the variables ´Band1´?

Upvotes: 1

Views: 2401

Answers (1)

dl.meteo
dl.meteo

Reputation: 1766

Just mask the values with np.nan

kopi[kopi <=3] = np.nan

This should yield to white pixels in your plot. Please provide test data in the future.

Upvotes: 2

Related Questions