How to add both NAME and NUMBER description on Geo Pandas plot in Python?

Viewed 201

I try to create a map in GeoPandas in Python.

And I use below code:

fig, ax = plt.subplots(1, figsize = (20,10))
 
dane_mapa_woj.plot(column='liczba_logowan_w_wojewodztwie', ax=ax, cmap='YlOrRd', linewidth=0.8, edgecolor='gray', legend = True)

#dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NUMBER, xy=x.geometry.centroid.coords[0], ha='center', color = "black"), axis=1)
dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NAME, xy=x.geometry.centroid.coords[0], ha='center', color = "black"), axis=1)

ax.axis('off')
 
plt.show()

And my picture shows like below:

enter image description here

As you can see I have descriptions of each territoru by code: dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NAME, xy=x.geometry.centroid.coords[0], ha='center', color = "black"), axis=1)

Nevertheless I would like to have both NAME and NUMBER on my plot, but when I use bothe these codes:

dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NUMBER, xy=x.geometry.centroid.coords[0], ha='center', color = "black"), axis=1)
dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NAME, xy=x.geometry.centroid.coords[0], ha='center', color = "black"), axis=1)

I have a strong mess on my picture:

enter image description here

My question is: how can I manage the position of NAME and NUMBER descriptions on the plot? Could you change my code to decrease this mess? I would like to have something like for example:

POMORSKIE

5742

1 Answers

You can try using a new line command:

dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NAME +'\n' + x.NUMBER, xy=x.geometry.centroid.coords[0], ha='center', color = "black", weight='semibold'), axis=1)

Otherwise if you want to precisely locate the text, look into shifting the text through the xytext and textcoords parameters of ax.annotate()

dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NAME, xy=x.geometry.centroid.coords[0], xytext=(0,0), textcoords='offset pixels', ha='center', color = "black", weight='semibold'), axis=1)
dane_mapa_woj.apply(lambda x: ax.annotate(text=x.NUMBER, xy=x.geometry.centroid.coords[0], xytext=(0,10), textcoords='offset pixels', ha='center', color = "black", weight='semibold'), axis=1)
Related