Folium: Visualize data on a map with Python

Folium is a library that visualizes data on a map.

JavaScript Leaflet There is a library that supports interactive map processing Folium can be said to have parsed this library and made it available in Python.

Environment

I couldn't find the corresponding version on the Official page. Looking at the categories on pypi, it seems that both 2 and 3 systems will work.

Execute the following command in a somewhat new Python environment. It's'Easiest'.

folium installation


$ pip install folium

If you do not want to pollute the environment, separate it in advance with venv. I set up a disposable container.

FROM python:3.5.2-alpine

RUN pip install folium

Start container


#Create a folium image with Dockerfile in the current directory
docker build -t folium . 

#Launch the container from the folium image
docker run -it -v /home/nanakenashi/workspace:/workspace folium /bin/sh

To refer to the html file spit out from Folium The current directory (workspace) is mounted inside the container.

Try out

Thanks to QuickStart. First, start a Python interactive shell and load folium.

Preparation


$ python
Python 3.5.2 (default, Nov 17 2016, 22:46:45)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import folium

Try to make a map

Create a Map object with the coordinates and zoom level in one hand, name it and save it. The larger the zoom level, the higher the zoom level.

Create and save maps


>>> map1 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)
>>> map1.save('/workspace/map1.html')

Open the saved html file in your browser.

map_html.png

I wonder if the coordinates specified in location were the center I saw Australia off center, but I'm moving on.

Try to put a marker

Add marker


>>> map2 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)
>>> folium.Marker([-31.253218, 146.921099], popup='New South Wales').add_to(map2)
<folium.map.Marker object at 0x7fc1c73bd048>
>>> map2.save('/workspace/map2.html')

Open the saved map2.html.

map2_html.png

A marker has appeared. By the way, if you click it, the character string specified by popup will be displayed.

marker.png

Try to do something like visualization

Although various visualization options are used even within Quickstart This time, I will focus on the circular marker and put out something. First try.

Show circular marker


>>> map3 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)                                                                             
>>> folium.CircleMarker([-31.253218, 146.921099],
...                     radius=100000,
...                     popup='New South Wales',
...                     color='#3186cc',
...                     fill_color='#3186cc',
...                    ).add_to(map3)
<folium.features.CircleMarker object at 0x7fc1c7408a20>
>>> map3.save('/workspace/map3.html')

Open map3 in your browser.

map3_html.png

Since it seems that the quantity can be expressed by radius, let's reflect some numerical value for each city. At this point, I don't need to be interactive, so I'll write a script.

Place markers by city


import folium

map4 = folium.Map(location=[-30.159215, 138.955078], zoom_start=4)

#Stores data for each city (originally obtained from DB or csv file)
states = (
    {'lat': -35.473468, 'lon': 149.012368, 'value': 1, 'name': 'Australian Capital Territory'},
    {'lat': -31.253218, 'lon': 146.921099, 'value': 2, 'name': 'New South Wales'},
    {'lat': -19.491411, 'lon': 132.550960, 'value': 3, 'name': 'Northern Territory'},
    {'lat': -20.917574, 'lon': 142.702796, 'value': 4, 'name': 'Queensland'},
    {'lat': -30.000232, 'lon': 136.209155, 'value': 5, 'name': 'South Australia'},
    {'lat': -41.454520, 'lon': 145.970665, 'value': 6, 'name': 'Tasmania'},
    {'lat': -37.471308, 'lon': 144.785153, 'value': 7, 'name': 'Victoria'},
    {'lat': -27.672817, 'lon': 121.628310, 'value': 8, 'name': 'Western Australia'}
)

#Weights to make the size of the circle easy to understand
WEIGHT = 100000

#Add markers for each city (Because it is difficult to increase the number, batch addition is a future issue)
for state in states:
    folium.CircleMarker(
            [state['lat'], state['lon']],
            radius=state['value'] * WEIGHT,
            popup=state['name'],
            color='#3186cc',
            fill_color='#3186cc',
    ).add_to(map4)

map4.save('/workspace/map4.html')

After executing the above script, display map4.html in your browser.

map4_html.png

The contents of QuickStart just made it feel like that. If you want to use it more solidly, see the Folium documentation It seems good to check the original Leaflet document as well.

Summary

I think that Google Maps is often used when providing as a service. Such libraries are also useful for research and internal offerings. In the article, it was limited to superficial visualization until the marker was placed. You can make various expressions such as editing the map itself and arranging polygons.

Jupyter Notebook seems to be able to analyze more interactively If you have used it, please give it a try.

Recommended Posts

Folium: Visualize data on a map with Python
Visualize grib2 on a map with python (matplotlib)
Add a Python data source with Redash
A memo with Python2.7 and Python3 on CentOS
I made a Hex map with Python
Try drawing a map with python + cartopy 0.18.0
[Python] Visualize overseas Japanese soccer players on a map as of 2021.1.1
Visualize prefectures with many routes by prefecture on a Japanese map
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Data analysis with python 2
Visualize data with Streamlit
Build a python environment with ansible on centos6
A story stuck with handling Python binary data
[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.
Decrypt a string encrypted on iOS with Python
Try drawing a map with Python's folium package
Data analysis with Python
Extract data from a web page with Python
Location information data display in Python --Try plotting with the map display library (folium)-
[Python] Plot data by prefecture on a map (number of cars owned nationwide)
[Grasshopper] When creating a data tree on Python script
Visualize and understand Japan's regional mesh on a map
Get financial data with python (then a little tinkering)
I tried to draw a route map with Python
Visualize B League goals and goals on a heat map
Make a breakpoint on the c layer with python
I made a Python3 environment on Ubuntu with direnv.
A server that echoes data POSTed with flask / python
A note on speeding up Python code with Numba
Sample data created with python
Get Youtube data with python
[Python] Notes on data analysis
Make a fortune with Python
Create a directory with python
What is a python map?
Read json data with python
A memo that reads data from dashDB with Python & Spark
Create a list in Python with all followers on twitter
Notes on importing data from MySQL or CSV with Python
Notes on handling large amounts of data with python + pandas
[Treasure Data] [Python] Execute a query on Treasure Data using TD Client
Build a python data analysis environment on Mac (El Capitan)
3. Natural language processing with Python 3-4. A year of corona looking back on TF-IDF [Data creation]
Building a Python environment on Mac
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Let's make a GUI with python.
Building a Python environment on Ubuntu
Solve ABC166 A ~ D with Python
Create a Python environment on Mac (2017/4)
[Python] Get economic data with DataReader
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Python data structures learned with chemoinformatics
Visualize python package dependencies with graphviz
Make a recommender system with python
Create a python environment on centos
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python