plotting netcdf data on a map in matplotlib

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

plotting netcdf data on a map in matplotlib

Jason Snyder
I am trying to plot data from a netcdf file onto a map through matplotlib.  I initially tried to use basemap but then someone suggested that I use cartopy based on the website:

http://scitools.org.uk/cartopy.

Either way I am able to generate the map fine but am having difficulty plotting the data successfully.  For instance using basemap I wind up getting a weird plot of data that does not make sense.  Also how to I go about plotting the netcdf data around the dateline?  Attached is the ncdump output (gridoutput.txt) for the netcdf file in terms of the grid and also the script that I am using to try to plot the data (netcdfplot.py). 
Can anyone give me some suggestions on how to go about fixing the errors that I have in the script?

-Jason

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

gridoutput.txt (5K) Download Attachment
erroneous_chart.png (23K) Download Attachment
netcdfplot.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: plotting netcdf data on a map in matplotlib

Jody Klymak
I’m not an expert on cartopy. 

1) can you plot this data w/o using basemap?
2) why do you have the “squeeze” command in there? 
3) I’m not clear what the problem is with your image from the snippet you sent.   Is it just an issue with the longitude wrapping?  Some projections demand longitude must be between -180 and 180. i.e. lon[lon>=180]=lon[lon>=180]-360.  You may also need to re-order your longitude and data so that longitude is monotonically increasing.
4) I recommend xarray when dealing w/ netcdf files, though maybe that is overkill for your problem.  

A minimal working example with a link to the bad data always helps.  

I’m sure with a bit of experimentation and use of google you can get this working.

i.e.

Cheers,   Jody





_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: plotting netcdf data on a map in matplotlib

Jason Snyder
The main issue I have is that the data does not plot properly.  There evidently is an issue of matching coordinates in the data versus the map.  I am sending the netcdf file along with the script.  I am not sure how to plot a map with data without using basemap since this is what provides the continental outlines.  If you know of any way I can plot this data using matplotlib successful let me know.

-Jason



On Thu, Mar 30, 2017 at 8:38 PM, Jody Klymak <[hidden email]> wrote:
I’m not an expert on cartopy. 

1) can you plot this data w/o using basemap?
2) why do you have the “squeeze” command in there? 
3) I’m not clear what the problem is with your image from the snippet you sent.   Is it just an issue with the longitude wrapping?  Some projections demand longitude must be between -180 and 180. i.e. lon[lon>=180]=lon[lon>=180]-360.  You may also need to re-order your longitude and data so that longitude is monotonically increasing.
4) I recommend xarray when dealing w/ netcdf files, though maybe that is overkill for your problem.  

A minimal working example with a link to the bad data always helps.  

I’m sure with a bit of experimentation and use of google you can get this working.

i.e.

Cheers,   Jody







--
Jason Snyder PhD


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

2017032800.nc.tar.gz (40K) Download Attachment
testnetcdf.py (40 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: plotting netcdf data on a map in matplotlib

Ryan May-3
Here's a version that works for me with cartopy. It relies on np.roll(), which just got added in numpy 1.12, to correct the fact that the longitudes are arranged 180-360, 0-180, which seems to be causing problems. The other easy way to solve that is to plot the data in chunks.

import cartopy.crs as ccrs
import cartopy.feature as cfeat
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import numpy as np

ncfile='/Users/rmay/Downloads/2017032800.nc'
fh = Dataset(ncfile)

lats = fh.variables['latitude'][:]
lons = fh.variables['longitude'][:]
HTSGW_var = fh.variables['HTSGW_surface']

roll_to = -lons.argmin()
lons = np.roll(lons, roll_to)
data = np.roll(HTSGW_var[:].squeeze(), roll_to, axis=-1)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_global()
lon, lat = np.meshgrid(lons, lats)
cs = ax.pcolor(lon, lat, data)
# If roll not available
#cs = ax.pcolor(lon[:, :180], lat[:, :180], data[:, :180])
#cs = ax.pcolor(lon[:, 180:], lat[:, 180:], data[:, 180:])

# draw coastlines.
ax.coastlines()
ax.add_feature(cfeat.LAND)
plt.show()



On Thu, Mar 30, 2017 at 2:49 PM, Jason Snyder <[hidden email]> wrote:
The main issue I have is that the data does not plot properly.  There evidently is an issue of matching coordinates in the data versus the map.  I am sending the netcdf file along with the script.  I am not sure how to plot a map with data without using basemap since this is what provides the continental outlines.  If you know of any way I can plot this data using matplotlib successful let me know.

-Jason



On Thu, Mar 30, 2017 at 8:38 PM, Jody Klymak <[hidden email]> wrote:
I’m not an expert on cartopy. 

1) can you plot this data w/o using basemap?
2) why do you have the “squeeze” command in there? 
3) I’m not clear what the problem is with your image from the snippet you sent.   Is it just an issue with the longitude wrapping?  Some projections demand longitude must be between -180 and 180. i.e. lon[lon>=180]=lon[lon>=180]-360.  You may also need to re-order your longitude and data so that longitude is monotonically increasing.
4) I recommend xarray when dealing w/ netcdf files, though maybe that is overkill for your problem.  

A minimal working example with a link to the bad data always helps.  

I’m sure with a bit of experimentation and use of google you can get this working.

i.e.

Cheers,   Jody







--
Jason Snyder PhD


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users




--
Ryan May


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Loading...