Forum Discussion
Add text in a shape map by regions
- 3 years ago
I solved my problem:
It's not possible with shape maps. For solve this problem, it's necessary to use R or Python Script.
- Firtsly, you have to drag the variables what you're going to use onto the python visual. For example, "regions" and "num_inhabitants"- Secondly, you need to read your geojson field and merging with default dataset (composed by dragged variables):
import geopandas as gpd import matplotlib.pyplot as plt # Reading geojson mapa = gpd.read_file('map.geojson') # Merging by the common column mapa_pedidos_venta = mapa.merge(dataset, left_on='region_name', right_on=dataset.region_name)Careful! It's essential that two columns that you're going to merge having exactly the same names. For example, "Catalonia" have to be on both map.geojson and the region field.
Then you can displaying the map and add labels with something like this:
# Plotting the map mapa_pedidos_venta.plot() #Adding labels for idx, row in mapa_pedidos_venta.iterrows(): labels = '{:.2%}'.format(row['inhabitants']) plt.annotate(text=etiqueta, xy=row['geometry'].centroid.coords[0], ha='center', fontsize=40) # Displaying the map plt.show()"geometry" is the name of the column of geojson file that contains the coordinates.
I solved my problem:
It's not possible with shape maps. For solve this problem, it's necessary to use R or Python Script.
- Firtsly, you have to drag the variables what you're going to use onto the python visual. For example, "regions" and "num_inhabitants"
- Secondly, you need to read your geojson field and merging with default dataset (composed by dragged variables):
import geopandas as gpd
import matplotlib.pyplot as plt
# Reading geojson
mapa = gpd.read_file('map.geojson')
# Merging by the common column
mapa_pedidos_venta = mapa.merge(dataset, left_on='region_name', right_on=dataset.region_name)
Careful! It's essential that two columns that you're going to merge having exactly the same names. For example, "Catalonia" have to be on both map.geojson and the region field.
Then you can displaying the map and add labels with something like this:
# Plotting the map
mapa_pedidos_venta.plot()
#Adding labels
for idx, row in mapa_pedidos_venta.iterrows():
labels = '{:.2%}'.format(row['inhabitants'])
plt.annotate(text=etiqueta, xy=row['geometry'].centroid.coords[0], ha='center', fontsize=40)
# Displaying the map
plt.show()
"geometry" is the name of the column of geojson file that contains the coordinates.