# Data plotting in a wrong place

Hi all! I'm trying to plot some sea ice freeboard data (netCDF, Gridded total
freeboard) on the Antarctic sea, but the data that should plot nicely around
Antarctica lies at the bottom of my image. NetCDF and matplotlib are fairly
new to me so I'm not quite sure, where the error could be and I feel like
I've search and tried everything there is.

from scipy.io.netcdf import netcdf_file as Dataset
import numpy as np
import matplotlib.pyplot as plt

FB = Dataset('./datasets/fb-0217-0320.nc', 'r')
f = FB.variables['f'][:,:]
lat = FB.variables['lat'][:,0]
lon = FB.variables['lon'][0,:]
mtx_lon, mtx_lat = np.meshgrid(lon, lat)
m = Basemap(projection='spstere',boundinglat=-50, lon_0=180.,
resolution='l')
m.bluemarble()

plt.figure()
plt.show()

And ncdump gives:
dimensions:
x = 79 ;
y = 83 ;
variables:
float lat(y, x) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude coordinate" ;
lat:units = "degrees_north" ;
float lon(y, x) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude coordinate" ;
lon:units = "degrees_east" ;
float f(y, x) ;
f:long_name = "total_freeboard" ;
f:units = "mm" ;
f:coordinates = "lat lon" ;

Could there be something funny with the projection or handling the data?
(When using meshgrid, handling the coordinates like ['lat'][:,0] seems
necessary, otherwise it turns lats and lons like (6557,6557) and gives error
message for pcolormesh, since masked_fb is (83,79).)

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Data-plotting-in-a-wrong-place-tp43580.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

don’t know if this would make a difference, but meshgrid here is completely unnecessary given that the netcdf file has the lats and lons in 2 dimensions anyway.

Given that this is a polar projection, I wouldn’t be surprised if there is something wonky there. Are the longitudes and latitudes monotonic?

Cheers!

Ben Root

···

On Thu, Jun 26, 2014 at 4:42 AM, billyi <bill.yi.wang@…32…> wrote:

Hi all! I’m trying to plot some sea ice freeboard data (netCDF, Gridded total

freeboard) on the Antarctic sea, but the data that should plot nicely around

Antarctica lies at the bottom of my image. NetCDF and matplotlib are fairly

new to me so I’m not quite sure, where the error could be and I feel like

I’ve search and tried everything there is.

from scipy.io.netcdf import netcdf_file as Dataset

import numpy as np

import matplotlib.pyplot as plt

FB = Dataset(’./datasets/fb-0217-0320.nc’, ‘r’)

f = FB.variables[‘f’][:,:]

lat = FB.variables[‘lat’][:,0]

lon = FB.variables[‘lon’][0,:]

mtx_lon, mtx_lat = np.meshgrid(lon, lat)

m = Basemap(projection=‘spstere’,boundinglat=-50, lon_0=180.,

resolution=‘l’)

m.bluemarble()

plt.figure()

plt.show()

And ncdump gives:

dimensions:

x = 79 ;

y = 83 ;

variables:

float lat(y, x) ;

``````lat:standard_name = "latitude" ;

lat:long_name = "latitude coordinate" ;

lat:units = "degrees_north" ;
``````

float lon(y, x) ;

``````lon:standard_name = "longitude" ;

lon:long_name = "longitude coordinate" ;

lon:units = "degrees_east" ;
``````

float f(y, x) ;

``````f:long_name = "total_freeboard" ;

f:units = "mm" ;

f:coordinates = "lat lon" ;
``````

Could there be something funny with the projection or handling the data?

(When using meshgrid, handling the coordinates like [‘lat’][:,0] seems

necessary, otherwise it turns lats and lons like (6557,6557) and gives error

message for pcolormesh, since masked_fb is (83,79).)

View this message in context: http://matplotlib.1069221.n5.nabble.com/Data-plotting-in-a-wrong-place-tp43580.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

Open source business process management suite built on Java and Eclipse

Turn processes into business applications with Bonita BPM Community Edition

Quickly connect people, data, and systems into organized workflows

Winner of BOSSIE, CODIE, OW2 and Gartner awards

http://p.sf.net/sfu/Bonitasoft

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Oh my, it WAS the meshgrid! Thank you so much!
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]

And plotting (without meshgrid!):

it works! Now I feel stupid.
And I think the longitudes and latitudes are not monotonic, but I don't know
the way to check this, other than checking the array like lon[:] in
terminal. Is there a better way?

And thank you again!
Bill Wang

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/Data-plotting-in-a-wrong-place-tp43580p43588.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

numpy slicing (subtract prior from next element check that 'all' the results are >=0):

In : import numpy

In : x=numpy.array([1,2,3,4,5])

In : (x[1:]-x[:-1])>=0
Out: array([ True, True, True, True], dtype=bool)

In : numpy.all((x[1:]-x[:-1])>=0)
Out: True

In : x=numpy.array([1,3,2,5,4])

In : numpy.all((x[1:]-x[:-1])>=0)
Out: False

···

On 06/27/2014 02:14 AM, billyi wrote:

And I think the longitudes and latitudes are not monotonic, but I don't know
the way to check this, other than checking the array like lon[:] in
terminal. Is there a better way?

Oh my, it WAS the meshgrid! Thank you so much!
lat = FB.variables['lat'][:,:]
lon = FB.variables['lon'][:,:]

And plotting (without meshgrid!):

it works! Now I feel stupid.
And I think the longitudes and latitudes are not monotonic, but I don't know
the way to check this, other than checking the array like lon[:] in
terminal. Is there a better way?

Yes. Consider:

all(lon[:-1] <= lon[1:])

If True, then lon is monotonically increasing. Otherwise it's not.

Description:

lon[:-1] is a slice that takes every element of lon except the last one.
lon[1:] is a slice that takes every element of lon except the first one.
The comparison operator will create a bool numpy array whose elements
will be True for each element "i" if the i'th element is less than or
equal to the i+1'th element. Applying the "all" (or numpy.all)
functions to this bool array will return True if every element is true
and False otherwise.

Faster, easier, and less error-prone than printing out the array and
checking it yourself. Of course you could do something more explicit:

monotonic = True
for i in range(len(lon)-1):
if lon[i] > lon[i+1]:
monotonic = False
break

HTH,
Jason

···

On Thu, 2014-06-26 at 23:14 -0700, billyi wrote:

actually, that is technically incorrect. That only works for monotonically increasing series, but not monotonically decreasing series.

diffs = np.diff(lon)

if np.all(diffs <= 0):

``````return True
``````

if np.all(diffs >= 0):

return True

return False

provided that len(lon) >= 2, obviously (and it doesn’t work right for 2 or more dimensions).

Ben Root

···

On Fri, Jun 27, 2014 at 10:03 AM, Jason Swails <jason.swails@…287…> wrote:

On Thu, 2014-06-26 at 23:14 -0700, billyi wrote:

Oh my, it WAS the meshgrid! Thank you so much!

lat = FB.variables[‘lat’][:,:]

lon = FB.variables[‘lon’][:,:]

And plotting (without meshgrid!):

it works! Now I feel stupid.

And I think the longitudes and latitudes are not monotonic, but I don’t know

the way to check this, other than checking the array like lon[:] in

terminal. Is there a better way?

Yes. Consider:

py> all(lon[:-1] <= lon[1:])

If True, then lon is monotonically increasing. Otherwise it’s not.

Description:

lon[:-1] is a slice that takes every element of lon except the last one.

lon[1:] is a slice that takes every element of lon except the first one.

The comparison operator will create a bool numpy array whose elements

will be True for each element “i” if the i’th element is less than or

equal to the i+1’th element. Applying the “all” (or numpy.all)

functions to this bool array will return True if every element is true

and False otherwise.

Faster, easier, and less error-prone than printing out the array and

checking it yourself. Of course you could do something more explicit:

py> monotonic = True

py> for i in range(len(lon)-1):

py> if lon[i] > lon[i+1]:

py> monotonic = False

py> break

HTH,

Jason

Open source business process management suite built on Java and Eclipse

Turn processes into business applications with Bonita BPM Community Edition

Quickly connect people, data, and systems into organized workflows

Winner of BOSSIE, CODIE, OW2 and Gartner awards

http://p.sf.net/sfu/Bonitasoft

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users