From 52a5e8a8acdcef3442877e2175fd3f66cc416017 Mon Sep 17 00:00:00 2001 From: Marianne Pietschnig Date: Thu, 22 Nov 2018 15:56:43 +0000 Subject: [PATCH 1/2] mp changes to ds_plots.py and new plotting function to call all necessary routines --- plotting_script.py | 20 ++++++++++++++++++++ tools/ds_plots.py | 9 ++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 plotting_script.py diff --git a/plotting_script.py b/plotting_script.py new file mode 100644 index 0000000..2105866 --- /dev/null +++ b/plotting_script.py @@ -0,0 +1,20 @@ +import xarray as xr +import matplotlib.pyplot as plt +import numpy as np +import cartopy.crs as ccrs +from cartopy.util import add_cyclic_point + +import sys +sys.path.insert(0, '/scratch/mp586/git_repos/ACDC/tools') +from ds_plots import * +from netCDF4 import Dataset + +# load dataset 1 +nc = Dataset('/scratch/mp586/git_repos/ACDC/Data/monthlyclimo_'+input('Which experiment ? ')+'.nc') +lats = nc.variables['lat'][:] +lons = nc.variables['lon'][:] + +P = xr.DataArray(nc.variables['precipitation'][:])*86400 + +mmap(np.asarray(P.mean('dim_0')),lats,lons,show = 'Yes') + diff --git a/tools/ds_plots.py b/tools/ds_plots.py index 3f1ab99..375da23 100644 --- a/tools/ds_plots.py +++ b/tools/ds_plots.py @@ -123,6 +123,12 @@ def mmap(mapdata,lat,lon,title=None,cb_ttl=None,cmap=None,clim=None, # cmap : special colormap # clim : colorbar limits, as a 2 element array + # mp added a few lines to convert potential xarray inputs to numpy arrays + # so that add_cyclic_point works + lon = np.asarray(lon) + lat = np.asarray(lat) + mapdata = np.asarray(mapdata) + if newfig: if do_zonal==1: # make 2 subplots, one for map, one for zonal average next to it @@ -140,7 +146,8 @@ def mmap(mapdata,lat,lon,title=None,cb_ttl=None,cmap=None,clim=None, cyclic_data, cyclic_lons = add_cyclic_point(mapdata,coord=lon) # Make the plot - cs = plt.pcolormesh(cyclic_lons,lat,cyclic_data,transform=ccrs.PlateCarree()) + #mp changed pcolormesh to contourf, get error otherwise + cs = plt.contourf(cyclic_lons,lat,cyclic_data,transform=ccrs.PlateCarree()) # set colormap if cmap: From 0a17ebab8020f674075280235ba6ea455df27f89 Mon Sep 17 00:00:00 2001 From: timothys-ekman Date: Wed, 28 Nov 2018 10:31:09 -0600 Subject: [PATCH 2/2] Added use_pcolor option, default to contourf but enable pcolor usage --- tools/ds_plots.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/ds_plots.py b/tools/ds_plots.py index 375da23..bb94667 100644 --- a/tools/ds_plots.py +++ b/tools/ds_plots.py @@ -110,7 +110,8 @@ def hmap(mapdata,lat,cmap=None,clim=None,title=None, def mmap(mapdata,lat,lon,title=None,cb_ttl=None,cmap=None,clim=None, filepath=None,show=None,sigmask=None,p=None, do_zonal=None,newfig=True, - outside_color = None, outside_val = None): + outside_color = None, outside_val=None, + use_pcolor=False): # Make a map (robinson projection) of mapdata over lat/lon # Inputs # mapdata : 2D field to be plotted @@ -122,6 +123,7 @@ def mmap(mapdata,lat,lon,title=None,cb_ttl=None,cmap=None,clim=None, # cb_ttl : colorbar title # cmap : special colormap # clim : colorbar limits, as a 2 element array + # use_pcolor: default is to use contourf. Set True for pcolormesh # mp added a few lines to convert potential xarray inputs to numpy arrays # so that add_cyclic_point works @@ -146,8 +148,12 @@ def mmap(mapdata,lat,lon,title=None,cb_ttl=None,cmap=None,clim=None, cyclic_data, cyclic_lons = add_cyclic_point(mapdata,coord=lon) # Make the plot - #mp changed pcolormesh to contourf, get error otherwise - cs = plt.contourf(cyclic_lons,lat,cyclic_data,transform=ccrs.PlateCarree()) + # Default: contourf + if not use_pcolor: + cs = plt.contourf(cyclic_lons,lat,cyclic_data,transform=ccrs.PlateCarree()) + else: + cs = plt.pcolormesh(cyclic_lons,lat,cyclic_data,transform=ccrs.PlateCarree()) + # set colormap if cmap: