Cartopy 中的轮廓与 ccrs.Mercator()

问题描述 投票:0回答:1
fig = pl.figure(figsize=(8, 8))
ax1 = fig.add_subplot(1, 1, 1, projection=ccrs.Mercator())

cf = ax1.contourf(lon, lat, chl ,levels=np.linspace(0,5,50),cmap=cmaps.MPL_rainbow,
                transform=ccrs.Mercator(),extend='both')
ax1.coastlines()
ax1.add_feature(cfeature.LAND)
ax1.set_extent([9,17,67.5,69.75])

lat_formatter = LatitudeFormatter(degree_symbol='')
lon_formatter = LongitudeFormatter(degree_symbol='')
ax1.gridlines(draw_labels={"left":"y","top":'x'},xformatter=lon_formatter,yformatter=lat_formatter)

cax = fig.add_axes([0.92,0.2,0.03,0.6])
fig.colorbar(cf, cax=cax, ticks=np.arange(0,6,5),shrink=0.7,orientation='vertical')

该图显示为空白,但如果我用 PlateCarree() 替换 Mercator(),则该图是正确的。

那么使用 ccrs.Mercator() 的contourf 出了什么问题?

python cartopy contourf
1个回答
0
投票

要得到正确的结果,坐标变换必须正确。在你的情况下,我怀疑位置数据是纬度/经度。如果我是正确的,您的错误是代码中指定的“transform=ccrs.Mercator()”。

这是一个演示代码,显示了正确的方法:

import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature

def sample_data(shape=(73, 145)):
    """Return ``lons``, ``lats`` and ``data`` of some fake data."""
    nlats, nlons = shape
    lats = np.linspace(-np.pi / 2, np.pi / 2, nlats) #(67.5+69.75)/2+10* 
    # lons is not complete 0-2pi here
    lons = np.linspace(0, 2 * np.pi, nlons) #(9+17)/2+10*
    lons, lats = np.meshgrid(lons, lats)
    wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
    mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)
    lats = np.rad2deg(lats)
    lons = np.rad2deg(lons)
    data = wave + mean
    return lons, lats, data

# Create a data set
# Location data units: decimal degrees == ccrs.PlateCarree()
lons, lats, data = sample_data()

# Plot specs, Mercator projection
fig = plt.figure(figsize=(8, 8))
ax1 = fig.add_subplot(1, 1, 1, projection=ccrs.Mercator())

cf = ax1.contourf(lons, lats, data,
                  levels=np.linspace(-1,1,50),
                  cmap='gist_rainbow',
                  transform=ccrs.PlateCarree(), 
                  extend='both', alpha=0.65, zorder=21)
ax1.coastlines(lw=0.3, zorder=20)
ax1.add_feature(cfeature.LAND, zorder=10)

# Use wider extent for clearer rendering
ax1.set_extent([-4+9, 4+17, -2+67.5, 2+69.75])
ax1.gridlines(draw_labels=True)

输出:

© www.soinside.com 2019 - 2024. All rights reserved.