Set folium map marker color based on a value in Python
In this tutorial, we will learn how to Add markers to the map using folium. We will also learn about how to set and change the color of the marker in the Folium map using Python.
Using pip in jupyter notebook or command prompt to install folium.
pip install folium
It will install the Folium library with the latest version.
You can check: Map visualization with Folium in Python
Create Basic Visualization with folium
import folium mymap = folium.Map(location=(22.54111111, 88.33777778) , zoom_start=10) mymap
The first value indicates Latitude and the second value is Longitude in the location parameter The above are the coordinates of Kolkata which is one of the the megacity in India. The zoom_start parameter will show us the map with the given zoom value. We can then zoom in or out depending on our requirements—more the value to the parameter the Zoom view to the map.
Output:
In the above map, we can clearly see the Kolkata city.
Let’s add markers to the map to make it more informative:
What is a marker?
The marker helps us to understand the map easily. We can easily see the information on the map. It gives us additional information about the place while reading the map. We can see this additional information when we put our cursor on the map.
mymap = folium.Map(location=(22.54111111, 88.33777778), zoom_start=10) folium.Marker( location=[22.54111111, 88.33777778],tooltip="I am a marker",popup="This is Kolkata",icon=folium.Icon(icon="cloud")).add_to(mymap) mymap
In the above code after visualizing the basic map we are using folium.marker
function to add a marker to the graph. Under this function we are using a tooltip to add additional information to that place when we put a cursor on the marker, a popup will show when we click on the marker. And then the icon to add an icon to the map. We are finally using the add_to
function and specifying the map name where we want to add this.
Output:
As we have not set any color to the marker yet. It is showing default color i.e. sky blue.
How to set the color to marker?
To change the color of the marker we need to modify the above code a little bit. Under the icon parameter, we will add a color parameter and specify the color in it. We can set any color value like ‘green’,’ blue’, ‘red’ etc.
mymap = folium.Map(location=(22.54111111, 88.33777778), zoom_start=10) folium.Marker( location=[22.54111111, 88.33777778],tooltip="I am a marker",popup="This is Kolkata",icon=folium.Icon(icon="cloud",color="green")).add_to(mymap) mymap
Output:
We can see the marker color has changed to green. You can try with other different colors.
Leave a Reply