user26750 user26750 - 1 year ago 131
Python Question

How to remove grey boundary lines in a map when plotting a netcdf using imshow in matplotlib?

Is it possible to remove the grey boundary lines around the in following map? I am trying to plotting a netcdf using


from netCDF4 import Dataset # clarify use of Dataset
import matplotlib.pylab as plt
fnc = Dataset(ncfile, 'r')
lat = fnc.variables['latitude'][:]
lon = fnc.variables['longitude'][:]
level = fnc.variables['level'][:]
mydata = fnc.variables['Data'][0, 0, :, :]
plt.figure(figsize = (8, 4))
imgplot = plt.imshow(mydata, cmap = 'YlGn')

enter image description here

Edit: I think the grey values are a result of missing values/no data.

Answer Source

Those gray boundaries are an interpolation artifact from imshow. To get rid of them, do:

imgplot = plt.imshow(mydata, cmap = 'YlGn', interpolation='none')

Or plot through Basemap and control drawing explicitly, as in this example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download