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

2 Answers 2

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, :])
Sign up to request clarification or add additional context in comments.

1 Comment

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 stackoverflow.com/questions/38338177/…
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.