Working with geographic data can be a complex task, but Python makes the process simpler thanks to several libraries that allow us to store, manipulate, and visualize geographic data in a more efficient way. In this tutorial, we’re going to learn how to make a map using Python.
Step 1: Install Required Libraries
To make maps, we are going to use two main Python libraries: Folium and Pandas.
You can install these libraries using pip as shown below:
1 |
pip install folium pandas |
Step 2: Load the Geographic Data
Geographic data often comes in the form of GeoJSON or Shapefile. These are formats that contain both data and geographic coordinates. For this tutorial, we will use a GeoJSON file for simplicity.
Let’s load the GeoJSON file using the pandas library:
1 2 |
import pandas as pd data = pd.read_json('path_to_your_file/file.geojson') |
Step 3: Create a Base Map
Now we create a base map using Folium. The starting point of this map (its center point) is determined by the latitude and longitude values.
1 2 |
import folium m = folium.Map(location=[45.523, -122.675], zoom_start=13) |
Step 4: Add the Geographic Data to the Map
We will add our GeoJSON data to this Folium map. Then, we can display it:
1 |
folium.GeoJson(data).add_to(m) |
This will display the map right within your Jupyter Notebook.
Full code
1 2 3 4 5 6 7 8 9 10 |
import folium # Create a map m = folium.Map(location=[45.523, -122.675], zoom_start=13) # Add the GeoJSON data directly to the map folium.GeoJson('file.geojson').add_to(m) # Display the map m.save('map.html') # Save the map to an HTML file |
Output (map.html)
Conclusion
Making maps in Python is easy and intuitive thanks to libraries like Folium. With just a few lines of code, we can create interactive and complex maps. From here, you can experiment with different types of data and create your own geo-visualizations.