【问题标题】:Cartopy map fill entire axisCartopy 地图填充整个轴
【发布时间】:2014-10-16 07:32:51
【问题描述】:

我想使用 Cartopy 在网格(LCC 投影中)上绘制数据,以便数据填充整个轴(还有轴,但这不是这里的问题)。

为了更清楚,这是我对 Cartopy 所做的:

import cartopy.crs as ccrs
import numpy as np
import pyproj as p4
from mpl_toolkits.basemap import Basemap

lalo = #read latitudes and longitudes of my grid defined in a special LCC projection (below)

lat = np.reshape(lalo[:,1],(ny,nx))
lon = np.reshape(lalo[:,0],(ny,nx))
minlat = lat[0,0]
maxlat = lat[-1,-1]
minlon = lon[0,0]
maxlon = lon[-1,-1]
Z = np.ones((ny,nx)) #some data

#grid definition for cartopy:
myproj = ccrs.LambertConformal(central_longitude=13.3333, central_latitude=47.5,
                               false_easting=400000, false_northing=400000,
                               secant_latitudes=(46, 49))
fig = plt.figure()
ax = plt.axes(projection = myproj)    
plt.contourf(lon, lat, Z)#, transform=myproj) 
#no difference with transform option as lon,lat are already in myproj projection

结果是一个没有填满整个轴的图像,但看起来像这样:

当像这样使用底图时:

a=6377397.155
rf=299.1528128
b= a*(1 - 1/rf)
m = Basemap(projection='lcc', resolution='h', rsphere=(a,b),
        llcrnrlon=minlon,llcrnrlat=minlat,urcrnrlon=maxlon,urcrnrlat=maxlat,
        llcrnrx=400000, llcrnry=400000,
        lat_1=46, lat_2=49, lat_0=47.5, lon_0=13.3333, ax=ax)
x,y = m(lon,lat)
m.contourf(x,y,Z)

我得到以下(所需)图像:

最后,当使用 proj4 使用这个定义 p4.Proj('+proj=lcc +lat_1=46N +lat_2=49N +lat_0=47.5N +lon_0=13.3333 +ellps=bessel +x_0=400000 +y_0=400000') 转换 lon 和 lat 时,我再次得到所需的图像:

是否有可能在 cartopy 中也实现这一点?

换句话说,我想要一个数据显示在一个完美的矩形中的图,并且背景图相应地扭曲,即与example相反的东西(无法安装 iris 包,否则我会尝试这个例子)

我尝试了一些方法,例如:

  • here 那样为我的投影构建一个自定义类,只是为了确保所有参数都设置正确(就像我的 proj4 定义中一样)。
  • 玩过aspect ratios,但它们只影响轴而不是轴, 还有其他一些事情。

非常感谢任何帮助!

【问题讨论】:

    标签: python matplotlib cartopy


    【解决方案1】:

    这里缺少的重要信息是您的 数据 采用纬度和经度,不是在笛卡尔横向墨卡托坐标系中。因此,您将需要使用表示经纬度的 笛卡尔 坐标系(此时尚未实现球形轮廓)。这样的坐标系以 PlateCarree crs 的形式存在 - 因此只需将其作为轮廓数据的变换传递即可将您的数据放在正确的位置。

    plt.contourf(lon, lat, Z, transform=ccrs.PlateCarree())
    

    这确实突出了一个事实,即数据的默认坐标系与地图的坐标系相同,在大多数情况下不是经度和纬度 - 更改 CRS 的唯一方法您的数据是通过传递 transform 关键字。

    HTH

    【讨论】:

    • 谢谢,这很好用。我的误解是我认为我在与数据相同的坐标系中创建了地图 (ax = plt.axes(projection = myproj))。
    • 是的 - 这就是像 Basemap 这样的工具所施加的限制。 Cartopy 允许您将您的绘图与数据的坐标系完全分开,它应该可以正常工作。
    猜你喜欢
    • 2021-07-11
    • 2020-03-20
    • 2022-08-18
    • 2019-12-05
    • 2019-07-06
    • 2016-01-15
    • 1970-01-01
    • 2015-03-22
    • 2018-06-10
    相关资源
    最近更新 更多