在底图上对齐数据(轮廓)

Aligning data (contourf) on Basemap

我已经开始使用底图,它似乎很有用。

如果我在 latitude/longitude 网格上绘制一些全局数据作为填充轮廓,效果很好:如果我将 lat_0 和 lon_0 保留为零。更改中心位置后,地图会移动但数据不会移动。我将不胜感激。

我创建了一个我正在使用的代码的简单版本,其中包含一些简单的样本数据来说明问题。这些值在赤道应该很大,但在两极应该很小。如果你 运行 带有 lat_0 和 lon_0 = 0 的代码,它工作正常。但是,如果您将中心位置更改为不同的坐标,即使地图已移动,也会显示相同的 pattern/data。

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

# create data 

lat = np.linspace(-90,90,num=180)
lon = np.linspace(-180,180,num=361)

h2o_north = np.linspace(1,65,num=90)
h2o_south = np.flipud(h2o_north)
h2o = np.append(h2o_north,h2o_south)

data = np.transpose(np.tile(h2o,(len(lon),1)))

# create figure and axes instances
fig = plt.figure(figsize=(10,10))
ax = fig.add_axes([0.1,0.1,0.8,0.8])

# create map
m = Basemap(projection='ortho',lon_0=-50,lat_0=50,resolution='l')

# draw coastlines and country boundaries
m.drawcoastlines()
m.drawcountries()
# draw parallels
parallels = np.arange(-90.,90,10.)
m.drawparallels(parallels)

# draw meridians
meridians = np.arange(180.,360.,10.)
m.drawmeridians(meridians)

ny = data.shape[0]
nx = data.shape[1]
lons, lats = m.makegrid(nx, ny) # get lat/lons of ny by nx evenly space grid
x, y = m(lons, lats)            # compute map projection coordinates

# draw filled contours.
clevs = np.linspace(0,70,num=281)
cs = m.contourf(x,y,data,clevs,cmap=plt.cm.jet)

# colorbar
cbar = m.colorbar(cs,location='bottom',pad="5%",ticks=np.linspace(0,70,15))
cbar.set_label('Scale of the data')

plt.title('Some global data', fontsize=14)

使用np.meshgrid()创建lon-lat的meshgrid,然后将其转换为投影坐标,数据就可以生成等高线和绘图了。

这是工作代码:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

# data for z (2D array)
h2o_north = np.linspace(1, 65, num=90)
h2o_south = np.flipud(h2o_north)
h2o = np.append(h2o_north, h2o_south)
data = np.transpose(np.tile(h2o, (len(h2o_north), 1)))

# create figure and axes instances
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot()

# create basemap instance
m = Basemap(projection='ortho', lon_0=-50, lat_0=50, resolution='c', ax=ax)

# create meshgrid covering the whole globe with ...
# conforming dimensions of the `data`
lat = np.linspace(-90, 90, data.shape[0])
lon = np.linspace(-180, 180, data.shape[1])
xs, ys = np.meshgrid(lon, lat)   # basic mesh in lon, lat (degrees)
x, y = m(xs, ys)                 # convert (lon,lat) to map (x,y)

# draw filled contours
clevs = np.linspace(0, np.max(data), 60)
cs = m.contourf(x, y, data, clevs, cmap=plt.cm.jet)
m.drawcoastlines()
m.drawcountries()

m.drawmeridians(range(-180, 180, 30))
m.drawparallels(range(-90, 90, 30))

# draw colorbar
cbar = m.colorbar(cs, location='bottom', pad="5%", ticks=np.linspace(0, np.max(data), 5))
cbar.set_label('Scale of the data')

plt.show()

结果图: