简体   繁体   English

蟒蛇。 在底图中绘制矩形

[英]Python. Draw rectangle in basemap

I need to add several rectangles in my basemap. 我需要在底图中添加几个矩形。 I nee four rectangles with lat and log ranges as below. 我选择了四个经纬度范围如下的矩形。

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

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

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

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

My script is below. 我的脚本在下面。 I found "polygon" packages to add lines but I do not exactly know how to do. 我找到了“多边形”包来添加行,但我不知道该怎么做。 Please help me!! 请帮我!! Thanks a lot for your help in advance! 非常感谢您的提前帮助!

    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)

Then I see "Type Error: 'tuple' object is not callable" 然后我看到“类型错误:'tuple'对象不可调用”

Instead of this part, I added one you suggested first. 除了这一部分,我还添加了您首先建议的内容。

    ..
    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()

Then I do not see any box in my plot. 然后,我在图中看不到任何方框。 I have to put another, not plt.show()?? 我必须放另一个,而不是plt.show()?

Also, would you let me know how to put number in the box (eg, 1 in the upper left conner of the box) ? 另外,您能让我知道如何在框中输入数字(例如,在框的左上角为1)吗? How to get sum of values in all points of my data and get percentage of (sume of values in the box) over (sum of values in all points of my data)? 如何获取数据所有点的值总和以及(框中的值总和)超过(数据的所有点的值总和)的百分比? I ask too much.. Just let me know what you can give me, it would be anyway great!!! 我问的太多了。只要让我知道你能给我什么,那还是很棒的!!!

THanks a lot!! 非常感谢!!

The tricky thing about this is that a 'rectangle' isn't really a 'rectangle' on many, many projection types. 棘手的是,在许多投影类型上,“矩形”并不是真正的“矩形”。 So when you say 'rectangle', do you mean an actual rectangle in map-space, or simply a rectangle in pixel-space? 因此,当您说“矩形”时,您是指地图空间中的实际矩形,还是像素空间中的矩形? The two require very different 两者要求非常不同

But let's assume you want it in map-space. 但是,假设您要在地图空间中使用它。 The quickest way is to just use Basemap's plot method, like so: 最快的方法是仅使用Basemap的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)

where bmap is your map, and lower_left etc. are lon-lat tuples at those corners. 其中bmap是您的地图, lower_left等是这些角处的lower_left元组。

Update with use examples: 使用示例更新:

You asked for a usage example, so here you go: 您询问了一个用法示例,因此您可以进行以下操作:

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

You can definitely do this more elegantly using list comprehensions to generate the correct corner-point sets, but this should get you started. 您绝对可以使用列表推导来生成正确的拐角点集,从而更加优雅地进行此操作,但这应该可以帮助您入门。

Update 2 更新2

So it appears there is some confusion here. 因此看来这里有些混乱。 plot_rec is a function . plot_rec是一个函数 It should be placed somewhere not inline with the rest of your script. 应该将其放置在脚本其余部分不内联的位置。 By itself, it doesn't do anything. 就其本身而言,它什么也没做。 It does when you call it here: 当您在此处调用它时,它会执行以下操作:

...
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)

This function bellow should do what you need. 该功能可以满足您的需求。 bmap is your basemap object, and lonmin,lonmax,latmin,latmax defines your domain in latitude/longitude terms. bmap是您的底图对象,并且lonmin,lonmax,latmin,latmax以纬度/经度来定义您的域。 You need to call the plot_rectangle function after bmap has been generated in your code by a call to Basemap(...). 你需要调用plot_rectangle功能BMAP已经到底图(...)的调用在代码生成后

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)

If, instead, your are looking for rectangles in the map space, whose borders follows paralleles and meridians, the following function works for me. 相反,如果您正在地图空间中寻找矩形,其边界遵循平行线和子午线,则以下函数对我有用。 m is the basemap object. m是底图对象。

 def draw_screen_poly( minlat, maxlat, minlon, maxlon, m):
      lons=np.hstack((np.repeat(minlon,10),\
                      np.linspace(minlon,maxlon, num=10),\
                      np.repeat(maxlon,10),\
                      np.linspace(maxlon,minlon, num=10)))

      lats=np.hstack((np.linspace(minlat,maxlat, num=10),\
                      np.repeat(maxlat,10),\
                      np.linspace(maxlat,minlat, num=10),
                      np.repeat(minlat,10)))

      m.plot(y=lats,x=lons,latlon=True, lw=2, color='navy', alpha=0.8)

      x, y = m(lons, lats)
      xy = zip(x,y)
      poly = Polygon( np.asarray(xy), linewidth=3) 
      ax.add_patch(poly)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM