本文介绍了当投影在Cartopy坐标中时,为什么带有蒙版数组的pcolor会填充不想要的四边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对 防止乱码的pcolor(mesh)数据虚假水平线的后续问题 为什么带有蒙版数组的pcolor仍然填充连接到蒙版点的四边形,我该如何停止呢? .在常规坐标中,当我同时遮罩坐标和数据时,我可以在两部分中为环绕的坐标(例如经度)绘制pcolor,现在,在常规坐标中,我成功地避免了不必要的四边形的出现.但是,当我将其转换为地图坐标时,此解决方案失败:

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:

请注意,因为当前投影的中心经度为零,所以任何正经度都映射到任何正向地图坐标,反之亦然.当我另外掩盖经度等于±180的经度时,我仍然会遇到相同的情况.因此问题出在其他地方.在投影地图坐标中如何将pcolor分为两部分绘制?

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?

推荐答案

我的印象是,该代码旨在作为一种解决方法,用于根据实际上并没有很好地工作/根本没有?(?).这段代码试图做类似的事情来掩盖不同的区域,但是以某种方式不能产生期望的结果.

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.

现在,另一方面,遍布全球的小蜜蜂问题仅存在于pcolormesh中,而不存在于pcolor中.可能是由于两种情况下使用的网格划分都不相同.
因此,当使用pcolor时,该图看起来像期望的那样.

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

这篇关于当投影在Cartopy坐标中时,为什么带有蒙版数组的pcolor会填充不想要的四边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 00:04