Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

python - Why does pcolor with masked arrays fill undesired quadrangles when projected in cartopy coordinates?

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:

As desired

In map coordinates, the undesired quadrangles are back:

Not as desired

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...