[PYTHON] [For beginners] Script within 10 lines (1.folium)

[For beginners] Script within 10 lines (1. Plot map with folium)

If you use various libraries with python, I thought, "You can do a little thing with a little code, and you can make a little script with a little 5 steps, which is convenient." So I just listed python and other commands. I may come up with this, but I will post a 10-step script on an irregular basis.

As ** 1st **, I would like to post a script that plots a map around Ginza in 3 places with markers using "folium (a python library that plots a map from python using leaflet). I will. In this script, I plotted the following 3 places.

--Yurakucho Station: Location information [35.6749187,139.7606366] --Ginza Corridor-gai: Location information [35.6703699,139.7573871] --Ginza-SIX: Location information [35.6695908,139.7618857]

【environment】 LinuxMint19.3 python: 3.6.9 pip3: 9.0.1 pandas: 1.0.3 jupyter-lab: 2.1.0 folium: 0.10.1


** 1. Prepare location information ** -The location information of the place to plot was called with the map of google, and the character string of url was copied with the mouse. It was easier than checking "About this place" on google map because each place to plot geographically is close. ** ・ By the way, the latitude is "latitude" and the longitude is "longtude" **, and if you search, they will be displayed in this order. As an example, in the case of Corridor-gai, it was "latitude: 35.6749187, longitude: 139.7573871". We will store this value in each location in the data frame.

** The minimum syntax of folium is as follows. ** ** map = folium.Map (location = [latitude, longitude of reference point], zoom_start = initial magnification) map

** 2. Coding ** The code runs in jupyter and the map is plotted in jupyter.

jupyter



#Example of code to plot around Ginza
# 1.Library import

import folium
import pandas as pd

# 2.Storage of location information in data frame and processing of marker display
plot_location = pd.DataFrame({
    'ginza':['Yurakucho_station','GinzaCorridorSteet','GinzaSix'],
    'latitude':[35.6749187 ,35.6703699 ,35.66695908],
    'longtude':[139.7606366 ,139.7573871 ,139.7618857],})

map = folium.Map(location=[35.6749187,139.7606366], zoom_start = 15)
for i, r in plot_location.iterrows():
    folium.Marker(location=[r['latitude'], r['longtude']], popup=r["ginza"]).add_to(map)

# 3.Map plot
map

The map of the Ginza area is now plotted. The image below is a screen capture, but it is plotted in a jupyter notebook. Click on Corridor-gai. I think it depends on the environment, but I couldn't display it in Japanese.

The text in this pop-up is the place to place the marker, but in "2. Storage of position information in the data frame and processing of marker display", the part that describes each point corresponds. 'ginza':['Yurakucho_station','GinzaCorridorSteet','GinzaSix'],

The location information is stored in the "data frame", but if you pass it directly to folium, you can make it into a 3-line script. However, if you use a "data frame", you can prepare a data set and increase the number of bases. It seems that some of the ancestors who referred to it are stored in "csv" etc.

ginza_map.png

If you want to save the plotted map, you can output it to "html" by executing ** "save" additionally as shown below. ** **

save


#Write the plotted map in html
map.save("map_giza.html")

3. [Supplement] Environment

This "folium" is a very useful library, but it didn't work as expected depending on the environment. For reference, the following is information on the environment in which this script was executed. It is only information on the development environment, but it depends on the browser environment because it uses a web browser. In fact, some terminals weren't plotted elsewhere. I think it's probably a browser issue.

-A part of the output is deleted.

environment


less /etc/lsb-release

DISTRIB_ID=LinuxMint
DISTRIB_RELEASE=19.3
DISTRIB_DESCRIPTION="Linux Mint 19.3 Tricia"

(py3_env) $ pip3 --version
pip 9.0.1 from /home/xxx/py3_env/lib/python3.6/site-packages (python 3.6)
(py3_env)$ python -V
Python 3.6.9

$ pip3 show pandas
Name: pandas
Version: 1.0.3
Home-page: https://pandas.pydata.org
Requires: python-dateutil, pytz, numpy
 
(py3_env)$ pip3 show jupyterlab
Name: jupyterlab
Version: 2.1.0
Home-page: http://jupyter.org
Requires: notebook, tornado, jinja2, jupyterlab-server

(py3_env)$ pip3 show folium
Name: folium
Version: 0.10.1
Home-page: https://github.com/python-visualization/folium
Requires: numpy, jinja2, requests, branca

** The above is a map plot using folium. ** **

Recommended Posts

[For beginners] Script within 10 lines (1.folium)
[For beginners] Script within 10 lines (8. Plot map with folium [2]
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)
Script within 10 lines (2.vimrc)
[For beginners] Script within 10 lines (3. Data acquisition / csv conversion with datareader)
[For beginners] Script within 10 lines (5. Resample of time series data using pandas)
[For beginners] Script within 10 lines (7. Script that outputs csv from sqlite3 table with bash
Roadmap for beginners
Spacemacs settings (for beginners)
python textbook for beginners
Dijkstra algorithm for beginners
OpenCV for Python beginners
Learning flow for Python beginners
CNN (1) for image classification (for beginners)
Python3 environment construction (for beginners)
Overview of Docker (for beginners)
Python #function 2 for super beginners
Seaborn basics for beginners ④ pairplot
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
[For Kaggle beginners] Titanic (LightGBM)
Reference resource summary (for beginners)
Linux command memorandum [for beginners]
Convenient Linux shortcuts (for beginners)