如何在底图上绘制矩形 [英] How to draw rectangles on a Basemap

查看:123
本文介绍了如何在底图上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在底图上绘制填充矩形的方法.我可以使用drawgreatcircle方法轻松绘制矩形的边缘,但是我找不到真正填充这些矩形的方法(指定颜色和alpha).

I'm looking for a way to plot filled rectangles on a Basemap. I could easily draw the rectangle's edges using the drawgreatcircle method, but I cannot find a way to actually fill these rectangles (specifying color and alpha).

推荐答案

您可以将matplotlib.patches.Polygon()直接添加到轴上.问题是,您是要用矩形定义图坐标(图上的直线)还是用地图坐标(图上的大圆)定义坐标.无论哪种方式,都可以在地图坐标中指定顶点,然后通过调用底图实例(在下面的示例中为m())将其转换为绘图坐标,自己构建一个Polygon,然后将其手动添加到要渲染的轴上.

You can add a matplotlib.patches.Polygon() directly to your axes. The question is whether you want your rectangles defined the plot coordinates (straight lines on the plot) or in map coordinates (great circles on the plot). Either way, you specify vertices in map coordinates and then transform them to plot coordinates by calling the Basemap instance (m() in the below example), build a Polygon yourself, and add it manually to the axes to be rendered.

对于在绘图坐标中定义的矩形,下面是一个示例:

For rectangles defined in plot coordinates, here's an example:

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

def draw_screen_poly( lats, lons, m):
    x, y = m( lons, lats )
    xy = zip(x,y)
    poly = Polygon( xy, facecolor='red', alpha=0.4 )
    plt.gca().add_patch(poly)

lats = [ -30, 30, 30, -30 ]
lons = [ -50, -50, 50, 50 ]

m = Basemap(projection='sinu',lon_0=0)
m.drawcoastlines()
m.drawmapboundary()
draw_screen_poly( lats, lons, m )

plt.show()

对于在地图坐标中定义的矩形,请使用相同的方法,但是在转换为绘图坐标之前在地图空间中插入线.对于每个线段,您都必须执行以下操作:

For rectangles defined in map coordinates, use the same approach, but interpolate your line in map space before transforming to plot coordinates. For each line segment, you'll have to do:

lats = np.linspace( lat0, lat1, resolution )
lons = np.linspace( lon0, lon1, resolution )

然后将这些地图坐标转换为绘图坐标(如上所述,使用m()),并再次使用绘图坐标创建一个多边形.

Then transform these map coordinates to plot coordinates (as above, with m()) and again create a Polygon with the plot coordinates.

这篇关于如何在底图上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆