Reputation: 253
I am trying to plot data from a netcdf using Basemap
but I guess since the latitude indices are inverted I get a map that is upside down. How should I fix this? Thanks!
fnc = Dataset(ncfile, 'r')
lat = fnc.variables['latitude'][:]
lon = fnc.variables['longitude'][:]
level = fnc.variables['level']
mydata = fnc.variables['Data'][:]
imgplot = plt.imshow(mydata[0, 0, :, :])
imgplot.set_cmap('RdYlGn')
plt.colorbar()
plt.show
m = Basemap(llcrnrlon = -180, llcrnrlat = -90, urcrnrlon = 180, urcrnrlat= +90, resolution = 'l', epsg=4326)
x, y = m(lon, lat)
im = m.imshow(mydata[0, 0, :, :])
m.drawcoastlines()
plt.show()
Upvotes: 0
Views: 1650
Reputation: 918
I believe that plotting command needs to be aware of the map coordinates x,y. Try to replace the
im = m.imshow(mydata[0, 0, :, :])
with
m.pcolormesh(x,y,mydata[0,0,:,:])
and it should work.
Upvotes: 0
Reputation: 3453
First, note that you're currently reading in one dimension of Data
:
mydata = fnc.variables['Data'][:]
but later you're trying to extract slices of it as if it were 4D:
imgplot = plt.imshow(mydata[0, 0, :, :])
So, you'll want to read-in all 4 dimensions of Data
(perhaps those are time, level, lat, lon?):
mydata = fnc.variables['Data'][:,:,:,:]
and then reverse latitudes using the ::-1
syntax:
imgplot.plotimshow(mydata[0, 0, ::-1, :])
Upvotes: 1