如何使用python在伦敦地图上添加散点图

时间:2019-08-21 23:35:47

标签: matplotlib-basemap scatter

我正在尝试将此散布图绘制在伦敦地图上,但这是不正确的,这是我的代码,这些地图的尺寸​​不同,我想要一张地图,但我得到了2张。 这是我的数据帧(dfrt),以获取分散的信息:

commonName  id  lat lon placeType   url additionalProperties2   category    key sourceSystemKey value   modified
0   River Street , Clerkenwell  BikePoints_1    51.529163   -0.109970   BikePoint   /Place/BikePoints_1 {'$type': 'Tfl.Api.Presentation.Entities.Addit...   Description TerminalName    BikePoints  001023  2019-08-22T11:17:04.327Z
1   Phillimore Gardens, Kensington  BikePoints_2    51.499606   -0.197574   BikePoint   /Place/BikePoints_2 {'$type': 'Tfl.Api.Presentation.Entities.Addit...   Description TerminalName    BikePoints  001018  2019-08-22T11:10:02.34Z
2   Christopher Street, Liverpool Street    BikePoints_3    51.521283   -0.084605   BikePoint   /Place/BikePoints_3 {'$type': 'Tfl.Api.Presentation.Entities.Addit...   Description TerminalName    BikePoints  001012  2019-08-22T11:12:02.7Z
3   St. Chad's Street, King's Cross BikePoints_4    51.530059   -0.120973   BikePoint   /Place/BikePoints_4 {'$type': 'Tfl.Api.Presentation.Entities.Addit...   Description TerminalName    BikePoints  001013  2019-08-22T11:08:02.047Z
4   Sedding Street, Sloane Square   BikePoints_5    51.493130   -0.156876   BikePoint   /Place/BikePoints_5 {'$type': 'Tfl.Api.Presentation.Entities.Addit...   Description TerminalName

这就是我想要获得的情节:

fig = plt.figure(figsize=(20, 10))
m = Basemap(projection='lcc', resolution='h',
lat_0=51.53, lon_0=0.08,
width=1E6, height=1.2E6)
m.shadedrelief()
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')


a4_dims = (20, 10)
fig, ax = plt.subplots(figsize = a4_dims)

ax.scatter(dfrt['lat'], dfrt['lon'])

ax.set_title('Latitud y Longitud de la ubicación de BikePoints')
ax.set_xlabel('Latitud')
ax.set_ylabel('Longitud')

能请你帮我吗?

1 个答案:

答案 0 :(得分:0)

您创建了两次图形/轴,最终绘制了2个图形。这是基于您的相关代码:

# create figure and axes
fig = plt.figure(figsize=(20, 10))
ax1 = plt.gca()
m = Basemap(projection='lcc', resolution='h', \
    lat_0=51.53, lon_0=0.08,
    width=1E6, height=1.2E6, ax=ax1)
m.shadedrelief()
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')

# a4_dims = (20, 10)
# fig, ax = plt.subplots(figsize = a4_dims)

# uncomment to do scatter plot
#ax1.scatter(dfrt['lat'], dfrt['lon'])

ax1.set_title('Latitud y Longitud de la ubicación de BikePoints')
ax1.set_xlabel('Latitud')
ax1.set_ylabel('Longitud')

plt.show()

和(未完成的)情节:

enter image description here

编辑1

对于地图上的数据绘图,以下是使用简单数据进行绘图的代码:

# sample data
lons = np.array([-5.371475, -1.569707, -0.892185])
lats = np.array([51.211262, 52.819886, 55.122479])
x,y = m(lons, lats)

# any of these code will plot dots on the map
#ax1.plot(*m(lons, lats), 'ro')  # OK
#ax1.plot(x, y, 'ro')            # OK
ax1.scatter(x, y, 25)            # OK

对于来自熊猫数据框的数据,请尝试获取lonslats,如下所示:

lons = list(dfrt['lon'])
lats = list(dfrt['lat'])

然后按照上面的步骤做。