首页 文章

蟒蛇 . 在底图中绘制矩形

提问于
浏览
2

我需要在我的底图中添加几个矩形 . 我有四个带有lat和log范围的矩形,如下所示 .

1)llcrnrlon = -10,urcrnrlon = 10,llcrnrlat = 35,urcrnrlat = 60

2)llcrnrlon = 10.5,urcrnrlon = 35,llcrnrlat = 35,urcrnrlat = 60

3)llcrnrlon = 35.5,urcrnrlon = 52,llcrnrlat = 30,urcrnrlat = 55

4)llcrnrlon = -20,urcrnrlon = 35,llcrnrlat = 20,urcrnrlat = 34.5

我的脚本如下 . 我找到了“多边形”包来添加行,但我不知道该怎么做 . 请帮我!!非常感谢您的帮助!

from mpl_toolkits.basemap import Basemap

     m=basemaputpart.Basemap(llcrnrlon=-60, llcrnrlat=20, urcrnrlon=60, urcrnrlat=70, resolution='i', projection='cyl', lon_0=0, lat_0=45)

    lon1=np.array([[-180.+j*0.5 for j in range(721)]  for i in range(181)])
    lat1=np.array([[i*0.5 for j in range(721)]  for i in range(181)  ])
    Nx1,Ny1=m(lon1,lat1,inverse=False)

    toplot=data[:,:]
    toplot[data==0]=np.nan
    toplot=np.ma.masked_invalid(toplot)
    plt.pcolor(Nx1,Ny1,np.log(toplot),vmin=0, vmax=5)
    cbar=plt.colorbar()

    m.drawcoastlines(zorder=2)
    m.drawcountries(zorder=2)

    llcrnrlon = -10
    urcrnrlon =  10
    llcrnrlat =  35
    urcrnrlat =  60
    lower_left = (llcrnrlon, llcrnrlat)
    lower_right= (urcrnrlon, llcrnrlat)
    upper_left = (llcrnrlon, urcrnrlat)
    upper_right= (urcrnrlon, urcrnrlat)

    plot_rec(m, lower_left, upper_left, lower_right, upper_right)

然后我看到“类型错误:'元组'对象不可调用”

而不是这一部分,我首先添加了你建议的一个 .

..
    m.drawcoastlines(zorder=2)
    m.drawcountries(zorder=2)

    def plot_rec(m, lower_left, upper_left, lower_right, upper_right):
        xs = [lower_left[-10], upper_left[-10],
              lower_right[10], upper_right[10]]
        ys = [lower_left[35], upper_left[60],
              lower_right[35], upper_right[60]]
        m.plot(xs,ys,latlon=True)

    plt.show()

然后我在剧情中看不到任何方框 . 我要放另一个,而不是plt.show()??

另外,你能让我知道如何把数字放在盒子里(例如,盒子左上角的1)?如何在我的数据的所有点获得值的总和并得到(框中值的值)的百分比(我的数据的所有点的值的总和)?我问得太多了..让我知道你能给我什么,无论如何都会很棒!!!

非常感谢!!

2 回答

  • 1

    关于这一点的棘手问题在于,“矩形”在很多很多投影类型上并不是真正的“矩形” . 所以当你说'矩形'时,你的意思是 Map 空间中的实际矩形,还是像素空间中的矩形?这两者要求非常不同

    但是让's assume you want it in map-space. The quickest way is to just use Basemap' s plot方法,如下:

    def plot_rec(bmap, lower_left, upper_left, lower_right, upper_right):
        xs = [lower_left[0], upper_left[0],
              lower_right[0], upper_right[0],
              lower_left[0], lower_right[0],
              upper_left[0], upper_right[0]]
        ys = [lower_left[1], upper_left[1],
              lower_right[1], upper_right[1],
              lower_left[1], lower_right[1],
              upper_left[1], upper_right[1]]
        bmap.plot(xs, ys, latlon = True)
    

    其中 bmap 是你的 Map , lower_left 等是那些角落的lon-lat元组 .

    Update with use examples:

    你问了一个用法例子,所以你去了:

    m=basemaputpart.Basemap(llcrnrlon=-60, llcrnrlat=20, urcrnrlon=60, urcrnrlat=70, resolution='i', projection='cyl', lon_0=0, lat_0=45)
    
    # your other setting up the map code here
    
    # here I draw the first rectangle
    llcrnrlon = -10
    urcrnrlon =  10
    llcrnrlat =  35
    urcrnrlat =  60
    lower_left = (llcrnrlon, llcrnrlat)
    lower_right= (urcrnrlon, llcrnrlat)
    upper_left = (llcrnrlon, urcrnrlat)
    upper_right= (urcrnrlon, urcrnrlat)
    
    plot_rec(m, lower_left, upper_left, lower_right, upper_right) # This calls the function I defined before
    
    # Rinse and repeat for the other lat/lon combos
    
    plt.show()
    

    你绝对可以使用列表推导来更优雅地生成正确的角点集,但这应该可以让你开始 .

    Update 2

    所以看来这里有一些混乱 . plot_recfunction . 它应放置在与脚本其余部分不一致的位置 . 它本身并没有做任何事情 . 当你 call 在这里时它会这样做:

    ...
    upper_left = (llcrnrlon, urcrnrlat)
    upper_right= (urcrnrlon, urcrnrlat)
    # This next line is where we call the function
    plot_rec(m, lower_left, upper_left, lower_right, upper_right)
    
  • 3

    这个功能下面应该做你需要的 . bmap是您的底图对象,lonmin,lonmax,latmin,latmax以纬度/经度术语定义您的域 . 在通过调用Basemap(...)在代码中生成bmap之后,需要调用 plot_rectangle 函数 .

    def plot_rectangle(bmap, lonmin,lonmax,latmin,latmax):
        xs = [lonmin,lonmax,lonmax,lonmin,lonmin]
        ys = [latmin,latmin,latmax,latmax,latmin]
        bmap.plot(xs, ys,latlon = True)
    

相关问题