为了减轻问题described here,我试图将我的pcolor
图分成两个互补部分。我有分别对应于经度和纬度的X
和Y
数据(实际上,这是通过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")
现在,我遮盖所有正经的图看起来或多或少像我期望的那样:
但是我遮盖所有负经度的图仍然在整个轴上绘制四边形:
在第二个图中,我只想绘制与正经度相对应的四边形。为什么它仍与屏蔽值建立连接,如何阻止这种情况发生?
最佳答案
我看到对另一个问题的快速评论可能还不够清楚,但是通过掩蔽,我的意思是网格需要掩蔽而不是值。
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()
关于python - 为什么带有蒙版数组的pcolor仍然填充连接到蒙版点的四边形,我该如何停止呢?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46547310/