简体   繁体   English

如何从 csv 文件中命名 matplotlib/geopandas map 中的每个城市?

[英]How to name each city in a matplotlib/geopandas map from csv file?

I have a csv file containing the cities in senegal, theres a city column, i have already used the long and lat columns to plot the points, but now id like to have each point have its city name next to it, and maybe later more information.我有一个 csv 文件,其中包含塞内加尔的城市,有一个城市列,我已经使用长和纬度列来 plot 点,但现在我希望每个点旁边都有它的城市名称,也许以后会更多信息。 how can i do this with matplotlib?我怎么能用 matplotlib 做到这一点? thank you.谢谢你。

import geopandas as gpd
import matplotlib.pyplot as plt
import os
import pandas as pd

file = os.path.join("senegal_administrative","senegal_administrative.shp")

cities_file = os.path.join("senegal_administrative","sn.csv")

cities = pd.read_csv(cities_file)
senegal = gpd.read_file(file)

## THE MAP IS PLOTED HERE
axis = senegal.plot(color="lightblue",edgecolor = "black",figsize = (20,20))

def_geo = gpd.GeoDataFrame(cities,geometry = gpd.points_from_xy(cities.lng,cities.lat))
print(def_geo)

## I PLOT THE POINTS HERE
def_geo.plot(ax = axis,color = "black")

plt.show()

heres a dropbox link if you need the files, i really appreciate it: https://www.dropbox.com/sh/dr54pbc9a5zc5ke/AADMxnYHe4maAnwKArCsh1m8a?dl=0如果您需要这些文件,这里有一个保管箱链接,我真的很感激: https://www.dropbox.com/sh/dr54pbc9a5zc5ke/AADMxnYHe4maAnwKArCsh1m8a?dl=0 在此处输入图像描述

To annotate the map, you can use .annotate() function.要注释 map,您可以使用.annotate() function。 Here is the relevant parts of the code:以下是代码的相关部分:

axis = senegal.plot(color="lightblue",edgecolor = "black",figsize = (12,12))
for idx,dat in cities.iterrows():
    #print(dat.city, dat.lng, dat.lat)
    axis.scatter(dat.lng, dat.lat, s=10, color='red')
    axis.annotate(dat.city, (dat.lng, dat.lat))

Plot will be similar to this: Plot 将与此类似:

塞内加尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM