为什么带有蒙版数组的pcolor仍然填充连接到蒙版点的四边形,我该如何阻止它?

时间:2017-10-03 14:42:51

标签: python matplotlib

为了缓解问题described here,我试图在两个免费部分绘制我的pcolor图。我有XY数据分别对应经度和纬度(实际上,这被cartopy转换为投影坐标,但手头的问题与此无关)。经度可能会缠绕在反导体上,导致四边形在全球范围内被绘制。为了防止这种情况,我试图分别绘制这两个部分,如下图所示:

#!/usr/bin/env python3.6

from numpy import array, ma
from matplotlib.pyplot import figure, pcolor, savefig

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(lons, lats, ma.masked_where(lons>0, bts))
savefig("/tmp/ok.png")

figure()
pcolor(lons, lats, ma.masked_where(lons<0, bts))
savefig("/tmp/not_ok.png")

现在,我掩盖所有正经度的情节或多或少看起来像我期望的那样:

positive longitudes masked

但是我掩盖所有负经度的情节仍然在整个轴上绘制四边形:

negative longitudes masked

在第二个图中,我想画出与正经度相对应的四边形。为什么它仍然与屏蔽值建立连接,如何阻止这种情况发生?

1 个答案:

答案 0 :(得分:2)

我看到也许我对另一个问题的快速评论不够清楚,但是通过屏蔽我的意思是网格需要通过屏蔽而不是值。

from numpy import array, ma
import matplotlib.pyplot as plt

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

fig, (ax,ax2) = plt.subplots(ncols=2)
ax.pcolor(ma.masked_where(lons>0, lons), 
       ma.masked_where(lons>0, lats), 
       ma.masked_where(lons>0, bts))

ax2.pcolor(ma.masked_where(lons<0, lons), 
       ma.masked_where(lons<0, lats), 
       ma.masked_where(lons<0, bts))

plt.show()

enter image description here