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

时间:2018-06-20 03:36:49

标签: python matplotlib matplotlib-basemap

我已经开始使用Basemap,这似乎很有用。

如果我在纬度/经度网格上以填充轮廓绘制一些全局数据,则效果很好:Iff我将lat_0和lon_0保留为零。一旦更改了中心位置,地图就会移动,但数据不会移动。我将不胜感激。

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

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 :(得分:0)

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

这是工作代码:

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

相关问题