How to Make Stunning Interactive Maps with Python and Folium in Minutes

This article was first published on python – Better Data Science , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

Data visualization can be tricky to do right. If you have the design skills of a domestic turkey, this article might save the day. 

Folium is a great library that does most of the heavy lifting for you. You only need to bring in the data. Today you’ll make your first interactive map, showing historical earthquakes near Fiji.

You’ll see how easy Folium is to use and understand through a lot of hands-on examples. This article alone will serve you as an excellent basis for more advanced visualizations.

Today you’ll learn how to:

Make a basic map with Folium

To make any visualization with Folium, you’ll first have to install the library. Execute this line from the terminal:

pip install folium

With regards to the dataset, you’ll need something with geolocations (latitude and longitude). This dataset is a perfect candidate – it contains historical earthquake data near Fiji. Here’s how to load it with Python:

The first couple of rows look like this:

Head of Quakes dataset

Image 1 – Head of Quakes dataset (image by author)

Just what you need – an earthquake dataset with geolocations and corresponding magnitudes. That’s all you need to make your first Folium map. Here’s the code:

Values for the location parameter were chosen after a quick Google search. Parameters width and height are optional – so don’t use them if you don’t want to.

Here’s how your first map looks like:

Your first Folium map

Image 2 – Your first Folium Map (image by author)

It’s okay, but it looks a bit boring. Let’s see how to change the map type next.

Changing map type

Folium comes with a couple of built-in tilesets. The default one looks horrible at sea, so let’s change it. You can pass any of the following to the tiles keyword:

  • OpenStreetMap
  • Mapbox Bright
  • Mapbox Control Room
  • Stamen Terrain
  • Stamen Toner
  • Stamen Watercolor
  • CartoDB positron
  • CartoDB dark_matter

There are others, but they require an API key. That’s a bit out of the scope for today. Let’s see how Stamen Terrain looks like:

 

Changing map type

Image 3 – Changing map type (image by author)

Better – at least at sea.

The map still doesn’t display any earthquake data. Let’s change that next.

Adding and styling markers

You’ll need to write a loop to add markers. A goal is to iterate through the dataset and add a marker for every latitude and longitude pair. 

Sounds easy enough, so let’s do it:

Here are the results:

Adding markers to the map

Image 4 – Adding markers to the map

These look awful. I’m not a fan of opaque blue markers and hope you aren’t either.

What if you want to color the markers based on the magnitude? Here’s an idea:

  • If a magnitude is 5 or below, make the marker yellow and almost fully transparent (a lot of smaller earthquakes)
  • If a magnitude is above 5, make the marker read and 100% opaque (not so many of these)

Let’s declare a function called generate_color() that returns outline color, fill color, outline opacity, and fill opacity based on the magnitude. 

Everything else stays roughly the same, with some minor changes in the loop. You can also see how to change the size of the marker. It’s specified with the radius parameter:

Here’s the corresponding map:

Styling markers

Image 5 – Styling markers (image by author)

Now we’re getting somewhere! I’m a complete idiot when it comes to combining two or (god forbid) more colors. I’m sure you can do a better job here.

There’s still one problem – the map isn’t interactive. Sure, you can zoom and drag, but nothing happens when you click on a marker. Popups can change that.

Adding popups

Let’s say you want to see earthquake magnitude and depth each time you click on a marker. One way to approach this is by declaring a function that returns HTML code showing both.

The generate_popup() function does just that. It also bolds the keywords:

Here’s the corresponding map:

Adding popups to the map

Image 6 – Adding popups to the map (image by author)

It looks the same but behaves differently. Your map is now decently interactive.

Conclusion

That’s all you should know about making basic maps. There are more advanced things you can do, but this is enough to get you started.

The Folium library isn’t the only option for making maps. You can do mostly the same things with Plotly, which is a more “complete” data visualization library.

Which one do you prefer? Folium or Plotly? Let me know in the comment section.

Join my private email list for more helpful insights.

The post How to Make Stunning Interactive Maps with Python and Folium in Minutes appeared first on Better Data Science.

To leave a comment for the author, please follow the link and comment on their blog: python – Better Data Science .

Want to share your content on python-bloggers? click here.