0

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

enter image description here

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()

enter image description here

user26750
  • 259
  • 6
  • 15

2 Answers2

1

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, :])
N1B4
  • 3,377
  • 1
  • 21
  • 24
  • Thanks for the quick reply. I have a follow up question as well and would appreciate it if you could help me there as well http://stackoverflow.com/questions/38338177/how-to-remove-grey-boudary-lines-in-a-map-when-plotting-a-netcdf-using-imshow-in – user26750 Jul 12 '16 at 20:19
0

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.

kakk11
  • 898
  • 8
  • 21