Bubble-Maps in Python

Combine the power of Python with Folium(JS) to make interactive Bubble Maps

Khanmazhar
4 min readFeb 28, 2023

Python is a versatile language with an extensive set of tools for creating interactive visualizations. When it comes to data visualization in Python, the options go beyond just the most frequently used graphs and charts available in analytical libraries. By combining Python with other tools such as OpenCV, Seaborn, Foliom, and interactive plot libraries, you can create compelling visualizations that communicate your data insights effectively.

One type of visualization that has become increasingly popular in applications like Uber and Lyft is the bubble map. In this article, you’ll learn what bubble maps are and how to use them in your projects using Python coding. We’ll explore how to create interactive bubble maps using Python. By the end of this article, you’ll be equipped with the skills to create impressive data visualizations and communicate your insights effectively.

What is a Bubble Map?

A bubble map visualizes data through the use of bubbles or circles that are placed over a map to represent a specific location and its associated value. The size of the bubble represents the relative magnitude of the value being visualized.

Bubble maps are useful for displaying data that has a geographic component and for quickly conveying patterns or trends in the data.

Covid-19 spread visualized in the form of a bubble map

Folium

We will be using a Python library called Folium to make our bubble maps. Folium allows us to create interactive maps and visualizations in Python. It is built on top of the popular mapping library leaflet.js.

If you don’t have Folium installed, use the command below to install Folium:

pip install folium

After installing Folium, let’s create a very basic map using this library. Use the code below to create a map using Folium:

import folium

m = folium.Map(location=[45.5236, -122.6750])
m

This code outputs the following map:

Folium Map

Implementing Bubble Maps

Now that you have an understanding of what bubble maps are, we can go ahead with the implementation of bubble maps.

Import all the necessary libraries. For this walkthrough, you should import the following libraries:

import pandas #--> for reading the dataset
import folium #--> for making the maps

After importing the libraries, read the dataset and save it in a pandas dataframe. To do this step, use the code given below:

df = pd.read_csv('location_dataset.csv')
df

Running the above code should give you the following output:

Each entry in the dataframe shows a location with its coordinates and values. The values simply represent the magnitude of some metric in a particular location. It could be the number of events happening in a particular place or the number of cars etc.

After loading the dataset, run the code below to create a bubble map:

n = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=3)
for i in range(0,len(df)):
folium.CircleMarker(
location=[df.iloc[i]['lat'], df.iloc[i]['lon']],
popup=df.iloc[i]['name'],
radius=float(df.iloc[i]['value']),
color='#69b3a2',
fill=True,
fill_color='#69b3a2'
).add_to(n)
n

Below, you’ll find an explanation of each line of code:

  1. n = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=3) - This line creates a Folium map object called n with a starting location of latitude 20 and longitude 0. The map will use OpenStreetMap tiles and will start at a zoom level of 3.
  2. for i in range(0,len(df)): - This line starts a for loop that will iterate through the rows of a Pandas DataFrame called df. len(df) returns the number of rows in the DataFrame, so this for loop will iterate from 0 to len(df)-1.
  3. folium.CircleMarker( - This line creates a new CircleMarker object from the Folium library. A CircleMarker is a circular marker that can be added to a Folium map.
  4. location=[df.iloc[i]['lat'], df.iloc[i]['lon']] - This line sets the location of the CircleMarker to the latitude and longitude values of the ith row in the df DataFrame. The .iloc method is used to select the ith row, and the ['lat'] and ['lon'] keys are used to select the latitude and longitude columns, respectively.
  5. popup=df.iloc[i]['name'] - This line sets the text that will appear in a popup when the CircleMarker is clicked to the value of the 'name' column of the ith row of the df DataFrame.
  6. radius=float(df.iloc[i]['value']) - This line sets the radius of the CircleMarker to the value of the 'value' column of the ith row of the df DataFrame, converted to a float.
  7. color='#69b3a2' - This line sets the color of the CircleMarker to a hex color code.
  8. fill=True - This line sets the fill property of the CircleMarker to True, meaning that the circle will be filled in.
  9. fill_color='#69b3a2' - This line sets the fill color of the CircleMarker to a hex color code.
  10. ).add_to(n) - This line adds the CircleMarker to the n map object.
  11. n - This line returns the n map object, which will display the map with all the CircleMarker objects added to it.

After running the above code, you’ll get a map that looks something like below:

Bubble map in Python using Folium

This is just a starting point and you can come up with even more interesting maps by using the folium library. There’s a range of customization that you can make to your maps. Here’s the official link to the folium documentation.

Don’t miss out on more Python-related content — follow me for additional blogs.

--

--

Khanmazhar

Freelance Technical Writer and Data Analyst on Upwork. Reach out for consultations at khanmazhar9101@gmail.com