在底图中使用maskoceans时,一半的世界被遮盖了 [英] Half of the world masked when using maskoceans in Basemap

查看:77
本文介绍了在底图中使用maskoceans时,一半的世界被遮盖了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在绘制来自netCDF数据集的数据时,我想掩盖海洋.我遵循了中给出的出色说明.它适用于世界一半地区,但是无论如何,格林威治以西的所有区域都被遮盖了,包括海洋和陆地. 这是我的代码:

I would like to mask oceans when plotting the data from a netCDF dataset. I followed the great instructions given in the answer to this question. It works great for half of the world, but somehow, everything west of Greenwich is masked as well, both ocean and land. Here is my code:

import netCDF4
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import mpl_toolkits
from mpl_toolkits import basemap
from mpl_toolkits.basemap import Basemap, maskoceans

filename = 'myfile.nc'
vmin = 0.
vmax = 1

nc = netCDF4.Dataset(filename, 'r')
data = nc.variables['sum'][:]  
lats_1d = nc.variables['lat'][:]
lons_1d = nc.variables['lon'][:]
lons, lats = np.meshgrid(lons_1d, lats_1d)

labels = ['DJF', 'MAM', 'JJA', 'SON']
cmap = cm.RdYlBu
cmap.set_over('#00FF00')

my_dpi = 96

fig = plt.figure(figsize=(1200/my_dpi, 800./my_dpi))
for season in range(4):
    ax = fig.add_subplot(2, 2, season+1)
    map1 = basemap.Basemap(resolution='c', projection='kav7', lon_0=0)
    map1.drawcoastlines()
    map1.drawcountries()

    nc_new = maskoceans(lons,lats,data[season,:,:],resolution='c', grid = 1.25)
    datapc = map1.pcolormesh(lons, lats, nc_new,  vmin=vmin, vmax=vmax, cmap=cmap, latlon=True)

    plt.title(labels[season])

fig.tight_layout(pad=1, w_pad=1, h_pad=4)
ax = fig.add_axes([0.05, 0.52, 0.9, 0.025])
cb = plt.colorbar(cax=ax, orientation='horizontal', cmap=cmap,
                  extend='max', format="%.2f",
                  ticks=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])

plt.show()

我知道在此处,但始终没有得到答案,看来问题出在最后,就是将经纬度坐标与xy坐标相混淆.我尝试切换到x-y坐标,但是得到了相同的半图.知道这里会发生什么吗?

I know that a somewhat similar issue was raised here but never got answered, and it appears that in the end, the problem was mixing up lat-long coordinates with x-y ones. I tried switching to x-y coordinates but got the same half-map. Any idea of what can be happening here?

N.B.当使用datapc = map1.pcolormesh(lons, lats, data[season,:,:], vmin=vmin, vmax=vmax, cmap=cmap, latlon=True)绘制未屏蔽数据时,将绘制整个世界(陆地+海洋).

N.B. when plotting the unmasked data using datapc = map1.pcolormesh(lons, lats, data[season,:,:], vmin=vmin, vmax=vmax, cmap=cmap, latlon=True) the whole world is plotted (land + oceans).

推荐答案

您已经确定,未绘制经度为-180到0的点.假设它们在您的数据中,则由于某种原因,它们必须被屏蔽或丢弃.

As you've identified, the points with longitudes -180 to 0 are not being plotted. Assuming they're in your data, they must be being masked or discarded for some reason.

我的直觉是数据集经度从0-360而不是-180到180,这是评论.

My intuition was that the dataset longitudes ran 0-360 instead of -180 to 180, which was confirmed in the comments.

快速解决方案是添加

lons_1d[lons_1d>180]-=360

nc中拔出lons_1d之后.之所以有效,是因为lons_1d是一个numpy数组,并且使用了numpy

just after you pull out lons_1d from nc. This works because lons_1d is a numpy array and it uses numpy boolean array indexing (often called "fancy" indexing) to conditionally select the longitude values greater than 180 and subtract 360 from them.

请注意,如果省略了遮罩,则pcolormesh绘图将起作用,这看起来像是在maskoceans函数中包含换行符的错误,或者至少是意外行为.

As you note that the pcolormesh plot works if you omit the mask, this looks like a bug with wrapping in the maskoceans function, or at least unexpected behaviour.

作为参考-我认为您不是第一个遇到类似的包装"型口罩问题的人,我认为这 matplotlib github 上的问题看起来很相似.

For reference - I do not think you are the first to experience similar "wrapping" type issues with masks, I think this issue on the matplotlib github looks rather similar.

这篇关于在底图中使用maskoceans时,一半的世界被遮盖了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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