This is a followup question to preventing spurious horizontal lines for ungridded pcolor(mesh) data and why does pcolor with masked array still fill quadrangles connecting to masked points, and how do I stop this?. In regular coordinates, when I mask both the coordinates and the data, I can plot a pcolor for coordinates that wrap around, such as longitudes, in two parts, and now I succeed to not get undesired quadrangles when in regular coordinates. However, when I transform it to map coordinates, this solution fails:
#!/usr/bin/env python3.6
from numpy import array, ma
from matplotlib.pyplot import figure, pcolor, savefig, axes
lons = array([[ 100., 120., 140., 160., 180.],
[ 120., 140., 160., 180., -160.],
[ 140., 160., 180., -160., -140.],
[ 160., 180., -160., -140., -120.],
[ 180., -160., -140., -120., -100.],
[-160., -140., -120., -100., -80.]])
lats = array([[ 0., 10., 20., 30., 40.],
[ 0., 10., 20., 30., 40.],
[ 0., 10., 20., 30., 40.],
[ 0., 10., 20., 30., 40.],
[ 0., 10., 20., 30., 40.],
[ 0., 10., 20., 30., 40.]])
bts = array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]])
figure()
pcolor(ma.masked_where(lons>0, lons), ma.masked_where(lons>0, lats), bts)
pcolor(ma.masked_where(lons<0, lons), ma.masked_where(lons<0, lats), bts)
savefig("/tmp/ok.png")
# now with cartopy
import cartopy.crs as ccrs
proj = ccrs.Mollweide(central_longitude=0)
trans = proj.transform_points(ccrs.Geodetic(), lons, lats)
figure()
ax = axes(projection=proj)
ax.pcolormesh(ma.masked_where(lons>0, trans[:, :, 0]), ma.masked_where(lons>0, trans[:, :, 1]), ma.masked_where(lons>0, bts), transform=proj)
ax.pcolormesh(ma.masked_where(lons<0, trans[:, :, 0]), ma.masked_where(lons<0, trans[:, :, 1]), ma.masked_where(lons<0, bts), transform=proj)
savefig("/tmp/not_ok.png")
In regular coordinates, as desired:
In map coordinates, the undesired quadrangles are back:
Note that any positive longitude maps to any positive map coordinate and vice versa, because the central longitude for the current projection is zero. When I additionally mask longitudes equal to ±180 I still get the same situation. So the problem lies elsewhere. How can I plot the pcolor
in two parts while in projected map coordinates?
See Question&Answers more detail:
os