matplotlib底图绘制与地图上的点大小相对应的图例

时间:2016-10-21 19:22:03

标签: python matplotlib matplotlib-basemap

我使用matplotlib的底图功能在地图上绘制数据点。每个点由5kM半径内存在多少共生点来衡量。我想在底部放置一个与不同大小的爆发相对应的参考表,但是我无法弄清楚如何做到这一点。到目前为止,这是我的代码:

map = Basemap(llcrnrlon=-20.,llcrnrlat=-40,urcrnrlon=160.,urcrnrlat=40.,projection='cyl', lat_0=13.5317, lon_0=2.4604)
map.drawmapboundary(fill_color='paleturquoise')
map.fillcontinents(color='olivedrab',lake_color='paleturquoise')
map.drawcoastlines()
map.drawcountries()
map.drawstates()
used = set()
for i,j,k,n in DATA:
    if map.is_land(i,j):
        if k in used: continue
        used.add(k)
        alpha = 0.5
        if n == 1:
            alpha = 1

        n *= 3
        map.plot(i, j, marker='o',color='r',ms=n, alpha=alpha)

plt.show()

注意,DATA是4元组的列表。 4元组中的每个条目对应于(latitude, longitude, unique ID corresponding to points co-occuring within a 5x5 km square, number of points with the same uniq ID)

结果:

enter image description here

1 个答案:

答案 0 :(得分:2)

最明显的选择是首先在matplotlib中创建自定义标签和句柄,然后使用它们对自定义图例进行初始化。例如,如果我们选择五个点大小的“展示”样本,范围从1到5,您可能希望按照以下方式执行操作:

def outbreak_artist(size):
    """ Returns a single-point marker artist of a given size """
    # note that x3 factor corresponds to your
    # internal scaling within the for loop
    return plt.Line2D((0,), (0,), color='r', marker='o',
                      ms=size*3, alpha=alpha, linestyle='')

sizes = [1, 2, 3, 4, 5]

# adapted from https://stackoverflow.com/a/4701285/4118756
# to place the legend beneath the figure neatly
ax = plt.gca()
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1,
                 box.width, box.height * 0.9])

red_dots = [outbreak_artist(size) for size in sizes]
labels = ["{} outbreaks".format(size) for size in sizes]

ax.legend(red_dots, labels, loc='upper center',
          bbox_to_anchor=(0.5, -0.05), ncol=5)

plt.show()

umbrella_corps_ltd

我摆弄了传说中的位置,以便在this帖子后将其带出情节。

P.S。:我认为我在fig, ax = plt.subplots(figsize = (9.44, 4.76))之前运行了Basemap,以使图例大小与地图大小保持一致。