Bubble-Maps in Python
Combine the power of Python with Folium(JS) to make interactive Bubble Maps
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.
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:
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:
n = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=3)
- This line creates a Folium map object calledn
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.for i in range(0,len(df)):
- This line starts afor
loop that will iterate through the rows of a Pandas DataFrame calleddf
.len(df)
returns the number of rows in the DataFrame, so thisfor
loop will iterate from 0 tolen(df)-1
.folium.CircleMarker(
- This line creates a newCircleMarker
object from the Folium library. ACircleMarker
is a circular marker that can be added to a Folium map.location=[df.iloc[i]['lat'], df.iloc[i]['lon']]
- This line sets the location of theCircleMarker
to the latitude and longitude values of thei
th row in thedf
DataFrame. The.iloc
method is used to select thei
th row, and the['lat']
and['lon']
keys are used to select the latitude and longitude columns, respectively.popup=df.iloc[i]['name']
- This line sets the text that will appear in a popup when theCircleMarker
is clicked to the value of the'name'
column of thei
th row of thedf
DataFrame.radius=float(df.iloc[i]['value'])
- This line sets the radius of theCircleMarker
to the value of the'value'
column of thei
th row of thedf
DataFrame, converted to a float.color='#69b3a2'
- This line sets the color of theCircleMarker
to a hex color code.fill=True
- This line sets thefill
property of theCircleMarker
toTrue
, meaning that the circle will be filled in.fill_color='#69b3a2'
- This line sets the fill color of theCircleMarker
to a hex color code.).add_to(n)
- This line adds theCircleMarker
to then
map object.n
- This line returns then
map object, which will display the map with all theCircleMarker
objects added to it.
After running the above code, you’ll get a map that looks something like below:
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.