How to create maps with Python

Pedro Alvarado
4 min readMar 17, 2022

Python is a powerful programming language, and you can do many things with it, things that maybe you haven’t imagine. Have you thought about creating a map with python? Well, that’s what we’ll do here, I’ll show how to create a map with python.

Mexico City map created with folium (a python library)

Installing Folium

Folium is a python library used for visualizing geospatial data, in other words, to visualize or create maps. Folium uses another libraries that get installed get you install folium. So, to install folium you simply need to run:

Creating our first map

Once folium is installed, you can start using it.

Here, we created an instance of the class Map that comes from folium. Map receives a location, which is a list (or tuple) of 2 elements, the latitude and longitude. Map has a bunch of other interesting parameters. I added another called zoom_start, which defines how close or far is the view. If you run the code on a jupyter notebook, you don’t need the last line, but if you don’t, you need it. The last line of code saves the map in a html file.

Adding markers

Now, we have seen how easy is to create a map with python. But, maybe we want to add new information to the map to enrich it, one way to do that is with markers.

Markers allows us to point to a specific coordinates an add information there. Let’s see how to do this.

To create a marker we create a Marker instance from folium. As well as the map, the Marker has a lot of parameters, but the main is the location. Apart from the location we have popup, which it will be show when the marker is clicked. And also we have an icon, which is an Icon instance from folium. You can use the bootstrap icon names, since they’re built in at folium. The last method (add_to) is very important, since is the one that indicates that the marker will be added to a certain map. If we run the code above, we have the next thing:

Different forms of visualization

The are different ways of visualizing the map that can work for different purposes. Here, I’ll show you one more than the one that comes by default. In the documentation, you can find all the ways of visualizing the maps.

As you can see I added another two markers, one has the particularity to be a CircleMarker. But also to the map i added one parameter, which is tiles, which defines the style of the map. The default is OpenStreetMap, which we had already seen above, but in this time i changed it to “CartoDB dark_matter”, and it look something like this:

Personally I liked a lot this style, the dark looks amazing. But at the end of the day, the style of the map doesn’t care, what cares is that the map is visualizing as you need it.

Folium has a lot of more features that you can find in it’s documentation. This article was just a basic tutorial to show you the most elemental things. Now, you have another tool into your python arsenal. If this article helps you or did you find it interesting please give it a clap. See you next and thank you for reading me.

--

--