Data visualization is not merely science, it is an art. The way our human brain works, it is really easy to process information in the form of visualization. After almost 25 years into digital mapping and many companies using machine learning to collect mass amounts of data, data visualization is important more than ever.

In this article, we are going to visualize the Tesla Superchargers (charging stations) available at present in France. We will use Python, Folium, and Pandas to get the job done. Excited? Let’s get started.

First things First

Data — First things first, we are using Tesla Supercharger data by Government of France provided by Tesla. You can find the data file on the link above or in my GitHub repository. The data has a lot of columns but we are going to use only Latitude, Longitude, and Station names.

Libraries — We will be using Folium and Pandas, so if you haven’t installed them yet, do it by typing the following in your terminal.

pip install folium
pip install pandas

Step 1 — Create a base map

Alright, all the Tesla and Visualization fanatics, let’s get started. To visualize supercharger locations, we, of course, need a map. Otherwise, how are we gonna pinpoint them? So, the first thing we do is, we create a base map for a particular location (anywhere in the world). Folium takes map data from Open Street Map by default. Have a good look at the code.

#Import Library
import folium

#Create base map
map = folium.Map(location=[48.8566, 2.3522], zoom_start = 10)

#Save the map
map.save("map.html")

First, we import the Folium library. Following that, we create an object — in our case map — with the help of folium.Map() method. In that method, we pass the coordinates and the zoom level for our map. And at last, you save the map, goes without saying. I started with Paris, and don’t you think the map of Paris looks like a brain cell? Or is it just me? Anyway, let’s move to the next step.

Paris

Step 2 — Plot a Marker

Now that we have created a base map, we want to plot our supercharger location. How do we do that? It’s super easy. We use folium.Marker() method to define and customize the marker. You can add parameters like popup, change the icons, and many more. Read more about it here. And at last, with .add_to() method, you associate that marker with your map.

We added our Tesla Supercharger at Thiais as our first marker, which is a commune in the southern suburbs of Paris, and this is what it looks like.

#Import Library
import folium

#Create base map
map = folium.Map(location=[48.754865,2.373034], zoom_start = 10)

#Add Marker
folium.Marker(location=[48.754865,2.373034,], popup = "Tesla Supercharger Thiais", 
              icon=folium.Icon(color = 'green')).add_to(map)

#Save the map
map.save("map.html")

Step 3 — Multiple Markers

Will you just plot one marker? Of course not. You probably are going to plot more than one or more than 100, and we are going to do the same. But before going towards 100, let’s try to do just 2. That way we will have more idea of how it works.

#Import Library
import folium

#Create base map
map = folium.Map(location=[48.754865,2.373034], zoom_start = 10)

#Add Multiple Marker
for coordinates in [[48.754865,2.373034],[49.20858,2.605978]]:
    folium.Marker(location=coordinates, icon=folium.Icon(color = 'green')).add_to(map)
    
#Save the map
map.save("map.html")

As you can see, we are using a for loop. Yes, just a for loop. Programming ain’t always about complex codes, you can do unbelievably good when you have the basics right. So we are running a loop for two markers and the rest is really easy for you to interpret. Isn’t it? Let’s save the map and see how we did.

Multiple Markers

Step 4 — Using Data

We did plot 2 markers, now what about those 100s? Are you going to add them one by one? You can, but will you? Definitely not. Therefore, we will use existing data files which has latitude and longitude in it, and pass it in the for loop we created.

#Import Library
import folium
import pandas as pd

#Load Data
data = pd.read_excel("tesla-supercharger-france.xlsx")
latitude = data['Ylatitude']
longitude = data['Xlongitude']
station = data['ad_station']

#Create base map
map = folium.Map(location=[48.754865,2.373034], zoom_start = 6)

#Plot Marker
for latitude, longitude, station in zip(latitude, longitude, station):
    folium.Marker(location=[latitude, longitude], popup=str(station), icon=folium.Icon(color = 'green')).add_to(map)

#Save the map
map.save("map.html")

We will use Pandas as our data manipulation library. I am guessing you are familiar with Pandas, but if not, look at this 10-minute Pandas tutorial and come back here, I’ll wait for you.

Programming ain’t always about complex codes, you can do unbelievably good when you have the basics right.

Back? Perfect. After loading data with read_csv or read_excel (I prefer using excel for this particular tutorial because the CSV file is not maintained properly), we extract each column we need and store it in respective lists. And in the for loop, instead of manual coordinates, we provide the list of the same and run it from beginning to end. After running this file, this is what you will see on the map. Neat, huh?

All Tesla Supercharging stations in France

Step 5 — Custom Icon

But wait, even though it displays everything correctly, it doesn’t give that “Tesla” feel, you know what I mean? The clean look, the minimal design? Let us try to imitate the same. Rather than these green markers, let’s use the Tesla icon. And try to make the map background more clear or should I say minimal? That’s for you to decide.

#Import Library
import folium
import pandas as pd

#Load Data
data = pd.read_excel("tesla-supercharger-france.xlsx")
latitude = data['Ylatitude']
longitude = data['Xlongitude']
station = data['ad_station']

#Create base map
map = folium.Map(location=[48.754865,2.373034], zoom_start = 6, tiles = "cartodbpositron")

#Plot Marker with custom Icon
for latitude, longitude, station in zip(latitude, longitude, station):
    icon_path = "tesla-iconn.png"
    icon = folium.features.CustomIcon(icon_image=icon_path ,icon_size=(30,30))
    folium.Marker(location=[latitude, longitude], popup=str(station), icon=folium.Icon(color = 'green')).add_to(map)

#Save the map
map.save("map.html")

To add custom icons, first, you have to download one (transparent background preferred), set the path of icon in icon_path. After that, in Folium’s CustomIcon() method, provide the icon_path and the size of the icon. And you are set to go. Execute the file and take a look at changes.

Step 6— Beautify by Clustering

Looks better than before but there’s something we can use to make it even more beautiful — Clustering. Let’s cluster nearby Tesla superchargers so that you only see what you want to see.

With Folium’s MarkerCluster() method, we can cluster our markers and beautify the map. We just have to do one thing different here: Before this, we were adding our markers to the map, but now, we will add our markers to marker cluster and then add the marker cluster to the map.

#Import Library
import folium
import pandas as pd
from folium.plugins import MarkerCluster

#Load Data
data = pd.read_excel("tesla-supercharger-france.xlsx")
latitude = data['Ylatitude']
longitude = data['Xlongitude']
station = data['n_station']

#Create base map
map = folium.Map(location=[47.85183, 3.542802], zoom_start = 6, tiles = "cartodbpositron")
marker_cluster = MarkerCluster().add_to(map)


#Plot Marker with custom Icon and Marker Cluster
for latitude, longitude, station in zip(latitude, longitude, station):
    icon_path = "tesla-iconn.png"
    icon = folium.features.CustomIcon(icon_image=icon_path ,icon_size=(30,30))
    folium.Marker(location=[latitude, longitude], popup=str(station), icon = icon).add_to(marker_cluster)

#Save the map
map.save("map.html")

After adding all the markers to the clusters, it looks something way better than it did before.

The final visualization/ Live map here

And voila! You have created your visualization using Python and Folium. It wasn’t that hard, right?

Endnotes

Learning something new without apt resources can be hard sometimes. Today you learned how to manipulate data with pandas, what’s Folium and how you can visualize things with Python using Folium. Now that you know your way around folium, find datasets from different repositories or on Google Dataset Search, and visualize your own thing.

At Locale, we are big fans of geospatial data and passionate about the community. You can check out similar tutorials here in this series here:

A Guide to Kickstart into the Geospatial World.
A collection of the best data sources, open source tools and packages to get started.
Geospatial Clustering: Types and Use Cases
Deep dive into all the different kinds of clustering with their use cases.