首页 文章

在Basemap上对齐数据(contourf)

提问于
浏览
0

我已经开始使用Basemap了,这似乎非常有用 .

如果我将纬度/经度网格上的一些全局数据绘制为填充轮廓,则效果很好:Iff我将lat_0和lon_0保留为零 . 一旦我改变了中心位置, Map 就会移动,但数据却没有 . 我很感激你的建议 .

我已经创建了一个我正在使用的代码的简单版本,其中包含一些简单的示例数据来说明问题 . 这些值在赤道处应该是大的,但在极点处应该很小 . 如果你使用lat_0和lon_0 = 0运行代码,它可以正常工作 . 但是,如果将中心位置更改为其他坐标,即使 Map 已移动,也会显示相同的图案/数据 .

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)

1 回答

  • 0

    使用 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()
    

    由此产生的情节:

    enter image description here

相关问题