【问题标题】:Issue w/ image crossing dateline in imshow & cartopy在 imshow 和 cartopy 中带有图像跨越日期的问题
【发布时间】:2017-11-16 17:31:54
【问题描述】:

我正在尝试使用 cartopy、matplotlib 和 imshow 绘制等距(以纬度/经度为单位)数据的方形网格。数据跨越了日期,我在让地图正常工作时遇到了问题。

这是我的问题的一个例子:

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


lat = np.arange(6000)*0.02 + (-59.99)
lon = np.arange(6000)*0.02 + (85.01)

dat = np.reshape(np.arange(6000*6000),[6000,6000])

tran = ccrs.PlateCarree()
proj = tran

plt.figure(figsize=(8,8))

ax = plt.axes(projection=proj)
print([lon[0],lon[-1],lat[0],lat[-1]])
ax.imshow(dat, extent=[lon[0],lon[-1],lat[0],lat[-1]],transform=tran,interpolation='nearest')
ax.coastlines(resolution='50m', color='black', linewidth=2)
ax.gridlines(crs=proj,draw_labels=True)
plt.show()

tran = ccrs.PlateCarree(central_longitude=180)
proj = tran

plt.figure(figsize=(8,8))

ax = plt.axes(projection=proj)
print([lon[0]-180,lon[-1]-180,lat[0],lat[-1]])
ax.imshow(dat, extent=[lon[0]-180,lon[-1]-180,lat[0],lat[-1]],transform=tran,interpolation='nearest')
ax.coastlines(resolution='50m', color='black', linewidth=2)
ax.gridlines(crs=tran,draw_labels=True)
plt.show()

第一个图产生了这个图像,在 180E 处截断:

第二个修复了地图问题,但网格刻度现在是错误的:

我想我尝试过重新投影(其中 tran != proj),但它似乎要么挂起,要么花费的时间太长。

我基本上想要底部图像,但带有正确的标签。我将有更多的地理定位数据来覆盖,所以我想正确地做到这一点,而不是现在看起来像一个黑客。

【问题讨论】:

  • 没有cartopy,但你不能用ax.set_xticklabels把标签改成你想要的任何东西吗?
  • 对我的回答有什么意见/问题吗?

标签: python matplotlib cartopy


【解决方案1】:

使用 Cartopy,绘制跨越日期线的地图总是具有挑战性的。这是绘制您想要的地图的代码。

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

# demo raster data
n1 = 300
m1 = 0.4
lat = np.arange(n1)*m1 + (-59.99)
lon = np.arange(n1)*m1 + (85.01)
dat = np.reshape(np.arange(n1*n1), [n1,n1])

cm_lon=180  # for central meridian

tran = ccrs.PlateCarree(central_longitude = cm_lon)
proj = tran

plt.figure(figsize=(8,8))
ax = plt.axes(projection=proj)

ext = [lon[0]-cm_lon, lon[-1]-cm_lon, lat[0], lat[-1]]
#print(ext)

ax.imshow(dat, extent=ext, \
              transform=tran, interpolation='nearest')

ax.coastlines(resolution='110m', color='black', linewidth=0.5, zorder=10)

# this draws grid lines only, must go beyond E/W extents
ax.gridlines(draw_labels=False, xlocs=[80,100,120,140,160,180,-180,-160,-140])

# this draw lables only, exclude those outside E/W extents
ax.gridlines(draw_labels=True, xlocs=[100,120,140,160,180,-160])

plt.show()

生成的地图:

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2021-08-16
    • 1970-01-01
    • 2020-05-30
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 2022-01-07
    相关资源
    最近更新 更多