问题
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?
回答1:
I have the impression that the code that is meant to be a workaround for wrapping coordinates around the limits of the projection which was introduced into cartopy according to this issue is not actually working well/at all(?). This code tries to do a similar thing of masking the different regions, but somehow does not produce the desired result.
Now, on the other hand the issue of facets beeing wrapped around the globe is anyways only present in pcolormesh
, not in pcolor
; probably due to the different meshing used in both cases.
Therefore when using pcolor
the plot looks as desired.
import cartopy.crs as ccrs
proj = ccrs.Mollweide(central_longitude=0)
trans = proj.transform_points(ccrs.Geodetic(), lons, lats)
plt.figure()
ax = plt.axes(projection=proj)
ax.pcolor(ma.masked_where(trans[:, :, 0]>0, trans[:, :, 0]), ma.masked_where(trans[:, :, 0]>0, trans[:, :, 1]), ma.masked_where(trans[:, :, 0]>0, bts), transform=proj)
ax.pcolor(ma.masked_where(trans[:, :, 0]<0, trans[:, :, 0]), ma.masked_where(trans[:, :, 0]<0, trans[:, :, 1]), ma.masked_where(trans[:, :, 0]<0, bts), transform=proj)
plt.show()
来源:https://stackoverflow.com/questions/46548044/why-does-pcolor-with-masked-arrays-fill-undesired-quadrangles-when-projected-in