为什么带有掩码数组的 pcolor 仍然填充连接到掩码点的四边形,我该如何阻止? [英] why does pcolor with masked array still fill quadrangles connecting to masked points, and how do I stop this?

查看:55
本文介绍了为什么带有掩码数组的 pcolor 仍然填充连接到掩码点的四边形,我该如何阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了缓解

但是我遮盖了所有负经度的图仍然在整个轴上绘制了四边形:

在第二个图中,我只想绘制与正经度相对应的四边形.为什么它仍与屏蔽值建立联系,如何阻止这种情况发生?

解决方案

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

来自numpy导入数组的

 ,ma导入matplotlib.pyplot作为pltlons = 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]])图,(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()

To mitigate the problem described here, I'm trying to draw my pcolor plot in two complimentary parts. I have X and Y data that correspond to longitude and latitude, respectively (in reality, this is translated to projection coordinates by cartopy, but the problem at hand is independent of that). The longitude may wrap around the antimeridian, which causes quadrangles to be drawn all across the globe. To prevent this, I'm trying to draw the two parts separately, such as illustrated below:

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

Now, the plot where I mask all positive longitudes looks more or less as I would expect it to:

but the plot where I mask all negative longitudes, still plots quadrangles all over the axes:

In the second plot, I'd like to draw only quadrangles corresponding to positive longitudes. Why is it still making connections to masked values, and how can I stop this from happening?

解决方案

I see maybe I wasn't clear enough on my quick comment on the other question, but by masking I mean that the grid needs to by masked, not the values.

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

这篇关于为什么带有掩码数组的 pcolor 仍然填充连接到掩码点的四边形,我该如何阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆