Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Cartopy中的pcolormesh问题_Python_Matplotlib_Cartopy - Fatal编程技术网

Python Cartopy中的pcolormesh问题

Python Cartopy中的pcolormesh问题,python,matplotlib,cartopy,Python,Matplotlib,Cartopy,我正在尝试将Cartopy应用于北极的圆形南极赤平图,并向其中添加数据。我有几个问题 首先,在示例代码中,陆地要素添加到海洋要素之前。当我这么做的时候,我得到了一张只有海洋的地图。我在下面的代码中颠倒了调用顺序,得到了一张陆地和海洋的地图。为什么另一种秩序适用于南极的例子 第二,也是更重要的一点,我不明白为什么我的pcolormesh调用没有任何效果 我使用的是Python 2.7.7、matplotlib 1.5.1和Cartopy 0.15.1 import matplotlib.path

我正在尝试将Cartopy应用于北极的圆形南极赤平图,并向其中添加数据。我有几个问题

首先,在示例代码中,陆地要素添加到海洋要素之前。当我这么做的时候,我得到了一张只有海洋的地图。我在下面的代码中颠倒了调用顺序,得到了一张陆地和海洋的地图。为什么另一种秩序适用于南极的例子

第二,也是更重要的一点,我不明白为什么我的pcolormesh调用没有任何效果

我使用的是Python 2.7.7、matplotlib 1.5.1和Cartopy 0.15.1

import matplotlib.path as mpath
import matplotlib.pyplot as plt
import numpy as np

import cartopy.crs as ccrs
import cartopy.feature

lats = np.linspace(60,90,30)
lons = np.linspace(0,360,200)
X,Y = np.meshgrid(lons,lats)
Z = np.random.normal(size = X.shape)

def main():
    fig = plt.figure(figsize=[10, 5])
    ax = plt.subplot(1, 1, 1, projection=ccrs.NorthPolarStereo())
    fig.subplots_adjust(bottom=0.05, top=0.95,
                        left=0.04, right=0.95, wspace=0.02)

    # Limit the map to -60 degrees latitude and below.
    ax.set_extent([-180, 180, 60, 60], ccrs.PlateCarree())

    ax.gridlines()

    ax.add_feature(cartopy.feature.OCEAN)
    ax.add_feature(cartopy.feature.LAND)

    # Compute a circle in axes coordinates, which we can use as a boundary
    # for the map. We can pan/zoom as much as we like - the boundary will be
    # permanently circular.
    theta = np.linspace(0, 2*np.pi, 100)
    center, radius = [0.5, 0.5], 0.5
    verts = np.vstack([np.sin(theta), np.cos(theta)]).T
    circle = mpath.Path(verts * radius + center)

    ax.set_boundary(circle, transform=ax.transAxes)
    ax.pcolormesh(X,Y,Z,transform=ccrs.PlateCarree())


    plt.show()


if __name__ == '__main__':
    main()

您的代码留下了cartopy来指定地图上要素图的顺序,因此,一些要素可以在没有任何线索的情况下隐藏。可以明确指定绘图顺序

特征绘图的顺序由zorder控制,在大多数绘图语句中,zorder可以用
zorder=integer
指定。这是一个修改过的代码,可以生成更好的绘图

# your data
lats = np.linspace(60, 90, 30)
lons = np.linspace(0, 360, 160)
X,Y = np.meshgrid(lons, lats)
Z = np.random.normal(size = X.shape)

# new data for pcolormesh plot
latz = np.linspace(75, 90, 15)
lonz = np.linspace(0, 360, 160)
X1,Y1 = np.meshgrid(lonz, latz)
Z1 = np.random.normal(size = X1.shape)

def main():
    fig = plt.figure(figsize=[10, 10])
    ax = plt.subplot(1, 1, 1, projection=ccrs.NorthPolarStereo())
    fig.subplots_adjust(bottom=0.05, top=0.95,
                        left=0.04, right=0.95, wspace=0.02)

    # Limit the map to -60 degrees latitude and below.
    ax.set_extent([-180, 180, 60, 60], ccrs.PlateCarree())

    ax.gridlines()

    # zorder can be used to arrange what is on top
    ax.add_feature(cartopy.feature.LAND, zorder=4)   # land is specified to plot above ...
    ax.add_feature(cartopy.feature.OCEAN, zorder=1)  # ... the ocean

    # Compute a circle in axes coordinates, which we can use as a boundary
    # for the map. We can pan/zoom as much as we like - the boundary will be
    # permanently circular.
    theta = np.linspace(0, 2*np.pi, 100)
    center, radius = [0.5, 0.5], 0.5
    verts = np.vstack([np.sin(theta), np.cos(theta)]).T
    circle = mpath.Path(verts * radius + center)

    ax.set_boundary(circle, transform=ax.transAxes)
    # pcolormesh is specified to plot on top of the ocean but below land
    ax.pcolormesh(X1, Y1, Z1, transform=ccrs.PlateCarree(), zorder=3)

    plt.show()

if __name__ == '__main__':
    main()

看来这就是问题所在,谢谢。我可以问一下,为什么您决定使用不同的lat/lon栅格,为什么您选择图层1,3,4而不是0,1,2?我主要关注特征打印的顺序。如果我将您的数据用于
pcolormesh()
plot,所有海洋(和分划)都将被pcolormesh层隐藏。对于zorder的值,它们用于设置绘图顺序,从而允许我们生成所需的内容。值(1,3,4)可以产生与(0,1,2)不同或相同的输出。请注意,ax.gridlines()也可以有zorder=some_值,您可以使用可以猜测的值将其置于绘图顶部。为了获得更好的地图,还有其他因素要考虑。