how to add a label on each circle in a folium.circile map python

Merouane Hamdani picture Merouane Hamdani · Jun 26, 2018 · Viewed 11.6k times · Source

hei I am trying to add labels into folium.circle map but for some reasons it won t work , anyone could help, here is my map script :

import folium
# Make an empty map
m = folium.Map(location=[59.911491, 10.757933], tiles="Mapbox Bright", zoom_start=5)
# I can add marker one by one on the map
hc =list(rf_map["General HC Type"])
def color_producer(hc_type):
    if hc_type =="Oil Fields":
        return 'green'
    elif hc_type =="Oil & Gas Fields":
        return 'deeppink'
    else:
        return 'red'
for i,hc_map in zip(range(0,len(rf_map)),hc):
folium.Circle(
    location=[rf_map.iloc[i]['Latitude Dec Deg'],rf_map.iloc[i]['Longitude Dec Deg']],
    popup=rf_map.iloc[i]['Field Name'],
    radius=rf_map.iloc[i]['Oil Recovery PP Factor']*300,
    fill=True,
    fill_color=color_producer(hc_map),
        color=color_producer(hc_map),
    fill_opacity=0.7,
    label=rf_map.iloc[i]["Field Name"]
).add_to(m)
m.save('map.html')

Answer

korvinos picture korvinos · Jul 5, 2018

Hei. Do you want to have popup labels or just a text in the middle (or somewhere else) of a circle?

Popup:

To make a popup frame for a circle object you just need to use add_child method. The code will look like:

m = folium.Map([60, 10], tiles='Mapbox Bright', zoom_start=5)
folium.Circle([60, 10], 150000, fill=True).add_child(folium.Popup('My name is Circle')).add_to(m)

Output:

enter image description here

Regular text solution

If you find that the popup solution is not suitable for your needs, then you can just put a text object on a map. Coordinates of the text could be collocated with a circle center.

For example:

import folium
from folium.features import DivIcon


text = 'Test'
circle_lat = 60
circle_lon = 10

m = folium.Map([60, 10], tiles='Mapbox Bright', zoom_start=5)
folium.Circle([circle_lat, circle_lon], 150000, fill=True).add_child(folium.Popup('My name is Circle')).add_to(m)
folium.map.Marker(
    [circle_lat + 0.5, circle_lon - 1.6],
    icon=DivIcon(
        icon_size=(150,36),
        icon_anchor=(0,0),
        html='<div style="font-size: 24pt">%s</div>' % text,
        )
    ).add_to(m)
m

Output for that code is:

enter image description here

I found a solution for the text here. I hope that understood the question correctly and the examples helped to solve your issue. Please, let me know if have you have questions or I misunderstood something in the question.