Visualizing Tesla Superchargers in France Geospatially
Using Python and Folium, from scratch
--
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 foliumpip 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.
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.
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.
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.
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.
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.
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?
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.
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.
After adding all the markers to the clusters, it looks something way better than it did before.
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. Also, you can find all the code files, images, and data on my GitHub repository. If you have any doubts regarding this tutorial, my Twitter and Linkedin DMs are always open. Or just drop by digitally to say hi.
At Locale, we are big fans of geospatial data. We are building analytics software that helps companies analyze their on-ground performance using their geospatial data. On-demand companies can use this to answer questions like what is my demand-supply gap across every sq km and in real-time, where are my idle riders right now and where they should move, how much time does it take in each lap of the delivery and so on.
Our aim is to solve logistics and delivery problems “spatially” at scale by making geospatial data accessible, insightful and actionable. If you want to help us build Locale, we are hiring at Locale, apply here.