简体   繁体   中英

Adding label or location for geopandas plot

I have created a geopandas plot of a state with all it's administrative locations.

map_df

NAME    ADMIN_LEVE  geometry    Party
0   Valsad  5   POLYGON ((73.02324 20.72237, 73.04599 20.70275...   3
1   Mumbai  None    POLYGON ((72.76066 18.83622, 72.75976 19.18686...   2
2   Mumbai  5   POLYGON ((72.83468 19.04853, 72.84073 19.04861...   2
3   Daman   5   POLYGON ((72.85930 20.47104, 72.86580 20.46770...   2
4   Bharathiya Kamala Nagar None    POLYGON ((72.87127 19.02556, 72.87158 19.02717...   5
... ... ... ... ...
79  Dantewada   5   POLYGON ((80.82621 19.34821, 80.83647 19.33215...   4
80  Raj Nandgaon    5   POLYGON ((80.81506 20.76028, 80.81328 20.74423...   1
81  Kanker  5   POLYGON ((80.91854 20.09037, 80.89668 20.09483...   2
82  Bastar  5   POLYGON ((80.92032 19.31699, 80.89713 19.31387...   1
83  Durg    5   POLYGON ((80.84896 20.66974, 80.82175 20.68357...   4

This is the code which I use to create the plot of the state:

fig, ax = plt.subplots(1, figsize=(200, 100))
cmap = colors.ListedColormap(['red', 'blue', 'green', 'yellow', 'orange'])
bounds = [1,2,3,4,5,6]
norm = colors.BoundaryNorm(bounds, cmap.N)
ax.axis('off')
map_df.plot(column='Party', cmap=cmap, norm=norm,linewidth=0.8, ax=ax, edgecolor='0.8')
plt.show()

Current output:
电流输出

Desired output: would be the whole map marked the following way:
期望的输出

I'd like to change this image such that the values in map_df['NAME'] are labeled inside the plot of that administrative location.

Any help or suggestions would be really appreciated.

You can simply add a label like so

ax = map_df.plot(column='Party', cmap=cmap, norm=norm,linewidth=0.8, ax=ax, edgecolor='0.8')
ax.annotate(label, xy=(x, y), xytext=(3, 3), textcoords="offset points")

where the label is what you would want to add with the rest of the variables defined. Refer here for more on that.

I would also advise on using legends since text can overflow on regions with smaller area. You can see here for more on that - Customize Map Legends and Colors in Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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