Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22915

Real Python: Python Folium: Create Web Maps From Your Data

$
0
0

If you’re working with geospatial data in Python, then you might want to quickly visualize that data on a map. Python’s Folium library gives you access to the mapping strengths of the Leaflet JavaScript library through a Python API. It allows you to create interactive geographic visualizations that you can share as a website.

You’ll build the web map shown below, which displays the ecological footprint per capita of many countries and is based on a similar map on Wikipedia. Along the way, you’ll learn the basics of using Folium for data visualization.

In this tutorial, you’ll:

  • Create an interactive map using Folium and save it as an HTML file
  • Choose from different web map tiles
  • Anchor your map to a specific geolocation
  • Bind data to a GeoJSON layer to create a choropleth map
  • Style the choropleth map

If you work through the tutorial, then your interactive map will look like this in the end:

Countries by raw ecological footprint per capita

In this tutorial, you’ll create HTML files that you can serve online at a static web hosting service.

An alternative workflow is to use Folium inside of a Jupyter notebook. In that case, the Folium library will render your maps directly in the Jupyter notebook, which gives you a good opportunity to visually explore a geographical dataset or include a map in your data science report.

If you click below to download the associated materials to this tutorial, then you’ll also get a Jupyter notebook set up with the code of this tutorial. Run the notebook to see how well Folium and Jupyter can play together:

Free Source Code:Click here to download the free source code for building web maps in Python with Folium.

Take the Quiz: Test your knowledge with our interactive “Python Folium: Create Web Maps From Your Data” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Install Folium

To get started, create and activate a virtual environment and install folium and pandas. You can use the platform switcher below to see the relevant commands for your operating system:

PS> python-mvenvvenvPS> venv\Scripts\activate(venv)PS> python-mpipinstallfoliumpandas
$ python -m venv venv
$ source venv/bin/activate
(venv)$ python -m pip install folium pandas

You can use many features of Folium without pandas. However, in this tutorial you’ll eventually create a choropleth map using folium.Choropleth, which takes a pandas DataFrame or Series object as one of its inputs.

Create and Style a Map

A useful and beginner-friendly feature of Folium is that you can create a map with only three lines of code. The map looks good by default because the underlying Leaflet JavaScript library works well with a number of different tile providers, which provide high-quality map backgrounds for your web maps.

Note: A tile for a web map is an image or vector data file that represents a specific geographical area. Tiled web maps seamlessly join multiple tiles to present a geographical area that’s larger than a single tile.

Additionally, the library boasts attractive default styles for map features and gives you many options to customize the map to fit your needs exactly.

Display Your Web Map Tiles—in Style!

You want to show data on a world map, so you don’t even need to worry about providing any specific geolocation yet. Open up a new Python file in your favorite text editor and create a tiled web map with three lines of code:

importfoliumm=folium.Map()m.save("footprint.html")

When you run your script, Python creates a new HTML file in your working directory that displays an empty world map with the default settings provided by Folium. Open the file in your browser by double-clicking on it and take a look:

Folium world map with the default web map tiles from OpenStreetMap

Read the full article at https://realpython.com/python-folium-web-maps-from-data/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 22915

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>