在matplotlib中将lat / lon转换为x / y以进行imshow

时间:2017-03-21 16:30:17

标签: python matplotlib matplotlib-basemap

真是愚蠢的问题。我有一个全局网格化数据集,我想将其子集到某个纬度/经度范围(50N-80N,185E-230E)并将其置于墨卡托投影中。由于我正在使用imshow,我需要正确设置范围。我找不到我需要的例子。我显然不明白如何设置它。任何帮助表示赞赏。

mapproj = Basemap(projection = 'merc', resolution = 'l', lat_0=65,lon_0=207.5)

x0, y0 = mapproj(185.0,50.0)
x1, y1 = mapproj(230.0,80.0)

myplot = plt.imshow(datamasked.T, interpolation = 'nearest', cmap = my_cmap, \
norm = matplotlib.colors.Normalize(vmin = 0.0, vmax = 10.0, clip = False), \
extent = (x0, x1, y0, y1))

1 个答案:

答案 0 :(得分:1)

这看起来不错,唯一需要使用底图imshow方法。也就是说,您应该使用plt.imshow()

而不是mapproj.imshow()

这是一个完整的工作示例:

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

m = Basemap(llcrnrlon=-10,llcrnrlat=35,urcrnrlon=35,urcrnrlat=60.,
             resolution='i', projection='tmerc', lat_0 = 48.9, lon_0 = 15.3)

m.drawcoastlines()

#Madrid
x,y = m([-3.703889],[40.4125])
m.plot(x,y, marker="o", color="blue", label="Madrid", ls="")

# image background
x0, y0 = m(-10, 35)
x1, y1 = m(35, 60)
extent= (x0, y0, x1, y1) # lonmin, lonmax, latmin, latmax
m.imshow(np.random.rand(23,56), interpolation = 'nearest',
          extent = extent, alpha=0.4, cmap="terrain")


plt.legend()
plt.show()

enter image description here