Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - why does pcolor with masked array still fill quadrangles connecting to masked points, and how do I stop this?

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:

positive longitudes masked

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

negative longitudes masked

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...