当投影到卡通坐标系时,带有遮罩阵列的pcolor会填充不需要的四边形?

2024-04-26 21:22:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这是preventing spurious horizontal lines for ungridded pcolor(mesh) datawhy does pcolor with masked array still fill quadrangles connecting to masked points, and how do I stop this?的后续问题。在正则坐标系中,当我同时屏蔽坐标和数据时,我可以为环绕的坐标(例如经度)绘制一个pcolor,分为两部分,现在我成功地在常规坐标系中没有得到不想要的四边形。但是,当我将其转换为地图坐标时,此解决方案失败:

#!/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")

在常规坐标下,根据需要:

As desired

在地图坐标系中,不需要的四边形又回来了:

Not as desired

请注意,任何正经度映射到任何正映射坐标,反之亦然,因为当前投影的中心经度为零。当我另外遮住等于±180的经度时,我仍然得到同样的情况。所以问题出在别处。在投影地图坐标中,如何将pcolor分为两部分?在


Tags: importtrans地图wherearrayfigureprojbts
1条回答
网友
1楼 · 发布于 2024-04-26 21:22:28

我的印象是,代码本来是一个解决方案,围绕投影的界限包装坐标,这是根据this issue引入cartopy中的,但实际上并没有很好地工作。这段代码尝试做类似的事情来屏蔽不同的区域,但是不知怎么的,并没有产生期望的结果。在

现在,另一方面,环绕地球的刻面问题无论如何只存在于pcolormesh中,而不是{};这可能是由于这两种情况下使用的网格不同。
因此,当使用pcolor时,绘图看起来是理想的。在

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

相关问题 更多 >