Geopandas - 带有 ID 的绘图点
Geopandas - plot points with their IDs
我想在地图上绘制一个点列表,旁边是每个点的 ID,这就是我正在做的事情
dfPoints["id"] = dfPoints.index
geomertrySensores2 = [Point(xy) for xy in zip(dfPoints['longitud'], dfPoints['latitud'])]
crs = {'int':'epsg:4326'}
geoSensores = gpd.GeoDataFrame(dfPoints,crs=crs, geometry = geomertrySensores2)
# creating color map for categories
categories = np.unique(geoSensores["id"])
colors = np.linspace(0, 1, len(categories))
colordict = dict(zip(categories, colors))
geoSensores["Color"] = geoSensores["id"].apply(lambda x: colordict[x])
f, ax = pl.subplots(figsize=(20,15))
geoBase.plot(color = 'grey', alpha=0.4, ax = ax)
geoSensores.plot(ax=ax, markersize=20, color="blue", marker="o", column='id')
但是我无法在地图上添加每个点的id
怎么做呢?
我有255个点,所以我想在它旁边画每个点的id而不是使用调色板
我设法使用 matplotlib 解决了这个问题:
f, ax = pl.subplots(figsize=(120,90))
geoVias.plot(color = 'grey', alpha=0.4, ax = ax)
geoSensores.plot(ax=ax, markersize=20, color="blue", marker="o", column='id')
for index, row in geoSensores.iterrows():
x = row.geometry.centroid.x
y = row.geometry.centroid.y
pl.text(x, y, row.id, fontsize=10)
但地图饱和度很高,所以我使用了可以缩放的散景
p = figure(title="Ubicacion de sensores",
plot_height = 700 ,
plot_width = 900, )
def getPointCoords(row, geom, coord_type):
"""Calculates coordinates ('x' or 'y') of a Point geometry"""
if coord_type == 'x':
return row[geom].x
elif coord_type == 'y':
return row[geom].y
ubicacionSensores['x'] = ubicacionSensores.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
ubicacionSensores['y'] = ubicacionSensores.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
p_df = geoSensores.drop('geometry', axis=1).copy()
psource = ColumnDataSource(p_df)
p.circle('x', 'y', source=psource, color='red', size=7)
my_hover = HoverTool()
my_hover.tooltips = [('Sensor id', '@id')]
p.add_tools(my_hover)
def getLineCoords(row, geom, coord_type):
"""Returns a list of coordinates ('x' or 'y') of a LineString geometry"""
if coord_type == 'x':
return list( row[geom].coords.xy[0] )
elif coord_type == 'y':
return list( row[geom].coords.xy[1] )
geoVias['x'] = geoVias.apply(getLineCoords, geom='geometry', coord_type='x', axis=1)
geoVias['y'] = geoVias.apply(getLineCoords, geom='geometry', coord_type='y', axis=1)
m_df = geoVias.drop('geometry', axis=1).copy()
msource = ColumnDataSource(m_df)
p.multi_line('x', 'y', source=msource, color='grey', alpha=0.4, line_width=1)
show(p)
我想在地图上绘制一个点列表,旁边是每个点的 ID,这就是我正在做的事情
dfPoints["id"] = dfPoints.index
geomertrySensores2 = [Point(xy) for xy in zip(dfPoints['longitud'], dfPoints['latitud'])]
crs = {'int':'epsg:4326'}
geoSensores = gpd.GeoDataFrame(dfPoints,crs=crs, geometry = geomertrySensores2)
# creating color map for categories
categories = np.unique(geoSensores["id"])
colors = np.linspace(0, 1, len(categories))
colordict = dict(zip(categories, colors))
geoSensores["Color"] = geoSensores["id"].apply(lambda x: colordict[x])
f, ax = pl.subplots(figsize=(20,15))
geoBase.plot(color = 'grey', alpha=0.4, ax = ax)
geoSensores.plot(ax=ax, markersize=20, color="blue", marker="o", column='id')
但是我无法在地图上添加每个点的id 怎么做呢? 我有255个点,所以我想在它旁边画每个点的id而不是使用调色板
我设法使用 matplotlib 解决了这个问题:
f, ax = pl.subplots(figsize=(120,90))
geoVias.plot(color = 'grey', alpha=0.4, ax = ax)
geoSensores.plot(ax=ax, markersize=20, color="blue", marker="o", column='id')
for index, row in geoSensores.iterrows():
x = row.geometry.centroid.x
y = row.geometry.centroid.y
pl.text(x, y, row.id, fontsize=10)
但地图饱和度很高,所以我使用了可以缩放的散景
p = figure(title="Ubicacion de sensores",
plot_height = 700 ,
plot_width = 900, )
def getPointCoords(row, geom, coord_type):
"""Calculates coordinates ('x' or 'y') of a Point geometry"""
if coord_type == 'x':
return row[geom].x
elif coord_type == 'y':
return row[geom].y
ubicacionSensores['x'] = ubicacionSensores.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
ubicacionSensores['y'] = ubicacionSensores.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
p_df = geoSensores.drop('geometry', axis=1).copy()
psource = ColumnDataSource(p_df)
p.circle('x', 'y', source=psource, color='red', size=7)
my_hover = HoverTool()
my_hover.tooltips = [('Sensor id', '@id')]
p.add_tools(my_hover)
def getLineCoords(row, geom, coord_type):
"""Returns a list of coordinates ('x' or 'y') of a LineString geometry"""
if coord_type == 'x':
return list( row[geom].coords.xy[0] )
elif coord_type == 'y':
return list( row[geom].coords.xy[1] )
geoVias['x'] = geoVias.apply(getLineCoords, geom='geometry', coord_type='x', axis=1)
geoVias['y'] = geoVias.apply(getLineCoords, geom='geometry', coord_type='y', axis=1)
m_df = geoVias.drop('geometry', axis=1).copy()
msource = ColumnDataSource(m_df)
p.multi_line('x', 'y', source=msource, color='grey', alpha=0.4, line_width=1)
show(p)