Colouring masked data using iris.plot

320 views
Skip to first unread message

Rhian Rees-Owen

unread,
Jan 16, 2018, 6:02:25 AM1/16/18
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hello,

I'm trying to set a colour for masked data (land-sea mask and bathymetry) - currently it plots as white, and I would like it to be pale grey.

Using matplotlib, I can do this very easily:

cmap = cm.get_cmap('seismic')

cmap.set_under('#808080')


This method works fine for lat-depth/lon-depth plots where I can just use iris.plot with normal axes.

However, I run into trouble when making lat-lon plots - it doesn't seem to do anything at all (regardless of whether I use set_under, set_over or set_bad). I suspect it's because I'm using cartopy GeoAxes to make my lat-lon plots and it's somehow changing the way the fill data in the mask is read? I have tried these suggestions and had no joy.

I have attached a minimum working example which (for me) shows this problem. Any help would be greatly appreciated!
Minimum_workingexample_irisplot_Colormap.py

RuthC

unread,
Jan 17, 2018, 6:48:29 AM1/17/18
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
I had a play with this as I was curious.  The problem seems to come in when cartopy is reprojecting the map.  For example, this works fine, and I get the top plot in my attached image:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import iris
import iris.quickplot as qplt
import cartopy.crs as ccrs

cmap
= cm.get_cmap('seismic')
cmap
.set_bad('yellow')

fname
= iris.sample_data_path('orca2_votemper.nc')
cube
= iris.load_cube(fname)

ax1
= plt.subplot(2, 1, 1, projection=ccrs.PlateCarree())
qplt
.pcolormesh(cube, cmap=cmap, axes=ax1)

However, if I try it with a cube whose longitudes go from 0 to 360, like this:
fname = iris.sample_data_path('air_temp.pp')
cube
= iris.load_cube(fname)

ax2
= plt.subplot(2, 1, 2, projection=ccrs.PlateCarree())
qplt
.pcolormesh(cube, cmap=cmap, axes=ax2)

I get a warning and the bottom plot in my attached image.
/site-packages-path/cartopy/mpl/geoaxes.py:1544: UserWarning: The colormap's 'bad' has been set, but in order to wrap pcolormesh across the map it must be fully transparent.

As I understand it, the cmap.set_under method sets the colour for values below your first plotting_level, so perhaps the reason that worked when plotting other cubes was that the missing values were specified by a low number, rather than a mask?

Ruth


masked_eg.png

Rhian Rees-Owen

unread,
Jan 19, 2018, 9:15:02 AM1/19/18
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Thanks for responding, Ruth!

The data that I'm plotting come from the same netcdf and haven't been manipulated so the missing values should be the same regardless of whether I make a lat-lon or a lat-depth plot. I guess I can get around it by replacing the masked data with very high/low numbers, but I wondered if there was a work-around or something else that I'm missing. 
Reply all
Reply to author
Forward
0 new messages