Location information data display in Python --Try plotting with the map display library (folium)-

1. 1. At the beginning#

Location information that has many opportunities to play an active role, such as checking the flow of people when planning to open a store. With the spread of SNS and IoT devices, I think there will be more and more opportunities to utilize location information for analysis.

This time, I had the opportunity to recently plot location information on a map and visualize it. When I looked it up, it seems that it can be visualized really easily with python.

This time I tried the following using the census data.

-** Map display on Jupyter Notebook ** -** Location information plot ** -** Creating a heat map **

2. Reference site etc.

-Map display library (this time, a library called "Folium (v0.10.1)" is used)
https://python-visualization.github.io/folium/index.html
* "Folium" seems to work on the map called by OpenStreetMap, so please be careful about the data to be handled *
・ e-Stat: General counter for official statistics (acquisition of census data)
http://nlftp.mlit.go.jp/isj/index.html
・ Ministry of Land, Infrastructure, Transport and Tourism location reference information download service (latitude / longitude information acquisition)
https://www.e-stat.go.jp/

3. 3. Main item

-** folium.Map (params) : Create a map. - folium.Popup (params) : Display a popup. - folium.Marker (params) : Plot pins on the map. - folium.plugins.HeatMap (params) **: Create a layer for heatmap display.

4. Sample code

● First, import the library.

import pandas as pd
import folium

● Load the dataset created in advance.

df = pd.read_csv('my_data.csv', encoding='shift_jis')
df.head(10)

It's a dataset like this. my_data.png

● The basic use of folium, ・ Map display ・ Plot of location information ・ Display of pop-up I will try.

#Map object creation
map = folium.Map(location=[35.710402, 139.810668], zoom_start=18)

#Create pop-up(「show=Always displayed as "True")
p_up = folium.Popup('Tokyo Sky Tree', min_width=0, max_width=1000, show=True)

#Plot on a map object
folium.Marker(location=[35.710402, 139.810668], popup=p_up).add_to(map)

#Map display
map

map.png

● Finally, let's display the heat map. Import the plugin for heatmap display.

from folium.plugins import HeatMap

● The heat map can be displayed by passing latitude / longitude information as an argument. Adjust the radius and blur of each point to make it look a little better.

#Map object creation
map = folium.Map(location=[35.710402, 139.810668], zoom_start=5)

#Add heatmap layer to map object
HeatMap(df[['latitude', 'longitude']], radius=5, blur=5).add_to(map)

#Map display
map

map.png

● Currently, the data is meaningless just because the places with a large number of records and high density are red, so let's weight it with "H22-H27 population increase / decrease".

#Map object creation
map = folium.Map(location=[35.710402, 139.810668], zoom_start=5)

#Add heatmap layer to map object by adding population increase / decrease as an argument
HeatMap(df[['latitude', 'longitude', 'Number of population changes from 2010 to 2015 [People]']], radius=5, blur=5).add_to(map)

#Map display
map

map.png

The redness of areas with a large number of population changes has become stronger.

5. Finally#

how was it? Do you have an image of handling location information data?

Bicycle rental companies analyze the user's riding distance and riding form to make a decision to enter the motorcycle rental business, and the government analyzes the flow line at the time of the event to secure the evacuation guidance route. It is a wide range of data.

I hope you will try it out!

Recommended Posts