什么是Matplotlib的“ax1.add_collection(mylinecollection)”等效的底图?

问题描述 投票:0回答:1

我从关系数据库中提取纬度/经度。这些点是线段的顶点,并且数据库表被组织成使得可以基于行号对点进行分组并且基于点顺序号对这些点进行排序。具体而言,数据库表具有以下字段:纬度,经度,line_number,pt_order_number。

我把所有的线段都拉成了一个Line Collection。它使用Matplotlib ax1.add_collection(mylinecollection)按预期绘制。请注意,我使用了行集合而不是ax1.plot(long,lat)循环,因为有些数据集有数千行和数万个点(行集合更快更干净)。

但是,我需要使用Basemap绘制这些线条集合(这样我有一个很好的阴影浮雕和比例尺)。底图无法通过.add_collection(),我不确定替代方案是什么。我在底图教程中找不到答案或明显的替代方法(但也许我错过了它)。谢谢你的帮助。

现行代码:

# note linecollection variable produced in function; performs as expected with "ax1.add_collection(linecollection)"
latitude_range = [34.012965603200001, 34.721878621999998]
longitude_range = [-116.76151759999999, -116.33691841200002]

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from mpl_toolkits.basemap import Basemap

south, north = latitude_range[0], latitude_range[1]
west, east = longitude_range[0], longitude_range[1]
center = [(east+west)/2, (north+south)/2]

m = Basemap(llcrnrlon=west, llcrnrlat=south, urcrnrlon=east, urcrnrlat=north,
            resolution='c', epsg=4326,
            lon_0=center[0], lat_0=center[1])

m.arcgisimage(service = "World_Shaded_Relief", xpixels = 2000)
m.add_collection(linecollection) # throws error "AttributeError: 'Basemap' object has no attribute 'add_collection'"

plt.show()
python python-2.7 matplotlib matplotlib-basemap
1个回答
1
投票

提供以上评论作为答案:

底图使用轴:

fig, ax = plt.subplots()
m = Basemap(..., ax=ax) 

在这个轴上你可以调用你喜欢的任何方法,例如

ax.add_collection(...)

确保首先将线集合的坐标转换为底图单位。

© www.soinside.com 2019 - 2024. All rights reserved.