The world’s leading publication for data science, AI, and ML professionals.

Folium Mapping: Displaying Markers on a Map

A Short Guide on Adding Markers to a Folium Map in Python

Photo by GeoJango Maps on Unsplash
Photo by GeoJango Maps on Unsplash

Folium is a powerful Python library that makes it easy to visualise geospatial data. It utilises the power of Leaflet.js, which is popular and leading JavaScript library that can be used to create interactive maps that can be used across both desktop and mobile platforms.

The library has been designed in a way that makes it easy and simple to use whilst maintaining good performance.

There are many ways data can be displayed on a Folium map, including:

  • Markers (points and circles)
  • Heatmaps
  • Clusters
  • Chloropeth

In this short tutorial we are going to see how we can display both single and multiple markers on a Folium map.

Displaying Markers in Folium Python Tutorial

Installing Folium

If you don’t already have folium installed you can install it via pip:

pip install folium

or Anaconda:

conda install folium

Importing Folium

The first step is to import the libraries we are going to use.

For this tutorial we will be using pandas to load in the data from a CSV file, and Folium for displaying our data on a map.

Loading Well Position Data from CSV

The data we are using for this example comes from the Norwegian Petroleum Directorate website and contains the locations of all wells that have been drilled on the Norwegian Continental Shelf.

Data can be downloaded here:

https://factpages.npd.no/en/wellbore/tableview/exploration/all

The data is licensed under a NOLD 2.0 licence from the Norwegian Government, details of which can be found here: Norwegian Licence for Open Government Data (NLOD) 2.0.

After downloading the data we will now load it using the pandas function read_csv().

When we call upon the dataframe ( df ), we are presented with the following:

We can see that we have 5 columns:

  • Well Name – Name of the Well
  • Purpose – What the purpose was behind drilling the well
  • Completion Year – Year the well was completed
  • Latitude – Latitude position of the well in decimal units
  • Longitude – Longitude position of the well in decimal units

Display a Blank Folium Map

To display a basic map with Folium we need to call upon folium.map() . Within that class method we can pass in a number of arguments.

  • location – The position where the map will be centred upon
  • zoom_start – The initial zoom level of the map
  • control_scale – Whether the scale controls are displayed on the map

There are a number of other parameters we can use. If you want to find out more check out the help documentation on the folium.map class.

This returns the following map centred on the location specified above.

Base folium map centred over Norway. Image by the author.
Base folium map centred over Norway. Image by the author.

Adding a Single Marker to a Folium Map

The simplest way we can present data on a map is by using markers.

We can add a single marker to our map by calling upon folium.Marker and passing in the location of the point. In this example we will display the mean latitude and longitude of our data.

To make it appear on the map, we need to apply the .add_to() function and pass in our map object.

And when we call upon the map object we get the following map, with a marker at the mean location of all data points (centre of the map).

Folium map showing the mean location of all of our wells. Image by the author.
Folium map showing the mean location of all of our wells. Image by the author.

Changing the Colour of the Marker on a Folium Map

We can also change the colour of our markers by calling upon the [icon](https://python-visualization.github.io/folium/modules.html?highlight=icon#folium.map.Icon) argument within the Marker method and setting it equal to:

folium.Icon(color='red', icon='')

The marker colour can be any of the following:

'red', 'blue', 'green', 'purple', 'orange', 'darkred', 'lightred', 'beige', 'darkblue', 'darkgreen', 'cadetblue', 'darkpurple', 'white', 'pink', 'lightblue', 'lightgreen', 'gray', 'black', 'lightgray'

To remove any symbols within the Folium marker we can set icon equal to a blank string.

Once we have chosen the colour, we then we need to add it to our map by using .add_to(map).

This is what the full code looks like:

Which generates the following map with our marker colour changed to red.

Folium marker after changing the colour and removing the symbol. Image by the author.
Folium marker after changing the colour and removing the symbol. Image by the author.

Changing the Icon on the Marker on a Folium Map

In the above example we removed the icon, but if we wanted to use an icon of our own choice we can pass in one of the named symbols from the Bootstrap website below.

https://getbootstrap.com/docs/3.3/components/

As an example, we can set the icon to pushpin

Then when we run the code and view the map we can see the icon has been updated to a pushpin.

Folium marker after changing the symbol. Image by the author.
Folium marker after changing the symbol. Image by the author.

When we zoom in, we can have a clearer view of what the icon looks like.

Zoomed in view of our marker and selected symbol. Image by the author.
Zoomed in view of our marker and selected symbol. Image by the author.

Adding Multiple Markers to a Folium Map

We could continue adding markers one by one, which may be fine for a small dataset. But when you have a dataset like this one with over 2,000 rows we need to consider an alternative approach.

In the code below:

  • We call upon our map and change the zoom_start to 3, which will zoom the map out more than the previous maps
  • We then iterate over each row in the dataframe
  • We then create an iframe object, which allows us to have more control over the popup appearance and content. In this case we are just displaying the well’s name
  • Next we create the popup and pass the iframe object and dimensions of the popup
  • Finally, we take each row’s Latitude and Longitude and add them to the map

    When we run this and call upon our map object, we get the following map back with all of the locations of the offshore Norwegian wells.

Folium map after adding multiple markers. Image by the author.
Folium map after adding multiple markers. Image by the author.

We can click on any well to see the popup and the name of the well.

Folium popup in an iframe containing the name of the well. Image by the author.
Folium popup in an iframe containing the name of the well. Image by the author.

Controlling Folium Marker Colour by Category

Rather than having a single colour representing the Folium markers, we can change the colour depending on a property within our data.

In this example, we will change the colour of our marker based on the purpose of the well.

For this we need to setup a dictionary with the purpose and a related colour. In this case we have three categories: WILDCAT, APPRAISAL and WILDCAT-CCS which are assigned to red, green and blue respectively.

As this column contains a few missing values (nans), we need to setup a short try-except block on line 15 to 19.

To call upon these colours, we change the parameter for the color argument within the call to folium.Icon().

When we call upon the map, we now have our markers coloured by their purpose.

Folium map with markers coloured by category. Image by the author.
Folium map with markers coloured by category. Image by the author.

We can zoom in and check a marker to see what the pop says.

Folium map with markers coloured by category and custom popup. Image by the author.
Folium map with markers coloured by category and custom popup. Image by the author.

Summary

Folium provides a great platform on which to display geospatial data. It is easy and simple to use once you understand the basics of it. If you have time, it is worth exploring the other map types available within the Folium library.


Thanks for reading. Before you go, you should definitely subscribe to my content and get my articles in your inbox. You can do that here! Alternatively, you can sign up for my newsletter to get additional content straight into your inbox for free.

Secondly, you can get the full Medium experience and support me and thousands of other writers by signing up for a membership. It only costs you $5 a month, and you have full access to all of the amazing Medium articles, as well as the chance to make money with your writing. If you sign up using my link, you will support me directly with a portion of your fee, and it won’t cost you more. If you do so, thank you so much for your support!


Related Articles