Python: Map with markers with Folium ver. 2

Last Updated on March 28, 2022 by shibatau

I. What shall we learn?

The image is replaced. URLs are added to the scripts.

Let’s learn how to create a world map with markeres with Folium like this:

We can see the scripts here:

Map with markers with Python and Folium

We can learn the differences among Altir, Plotly, and Folium here:

Interactive Choropleth Maps in Python Using Altair, Plotly, and Folium

II.Scripts

The scripts in the first link convert all the data to objects and might get an error when you create a map. You should remove “,dtype=str” from the scripts in this case, though there must some reason for the conversions.

# import libraries
# import libraries
import pandas as pd
import folium
# create a data frame
data = pd.DataFrame({
   'lon': [-58, 2, 145, 30.32, 30.31, -4.03, -73.57, 36.82, -38.5],
   'lat': [-34, 49, -38, 59.93, 50.27, 5.33, 45.52, -1.29, -12.97],
   'name': ['Buenos Aires', 'Paris', 'Melbourne', 'St_Petersbourg', 'Kyiv', 'Abidjan', 'Montreal', 'Nairobi', 'Salvador'],
   'population': [2891082, 2165423, 5159211, 5351935, 2962180, 3677115, 1762949, 4397073, 2886698],
   'url':['https://en.wikipedia.org/wiki/Buenos_Aires',
          'https://en.wikipedia.org/wiki/Paris',
          'https://en.wikipedia.org/wiki/Melbourne',
          'https://en.wikipedia.org/wiki/Saint_Petersburg',
          'https://en.wikipedia.org/wiki/Kyiv',
          'https://en.wikipedia.org/wiki/Abidjan',
          'https://en.wikipedia.org/wiki/Montreal',
          'https://en.wikipedia.org/wiki/Nairobi',
          'https://en.wikipedia.org/wiki/Salvador,_Bahia'
          ]
          })
data

You can put variables on Pop-up texts and I have inserted the population of each city and the URL of the page of each city on Wikipedia.

# create an empty map
n = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=2)
# add markers
for i in range(0,len(data)):
    html=f"""
        <h1> {data.iloc[i]['name']}</h1>
        <p>You can use any html here! Let's do a list:</p>
        <ul>
            <li>Population {data.iloc[i]['population']}</li>
            <li>Item 2</li>
        </ul>
        </p>
        <p>And that's a <a href="https://en.wikipedia.org/wiki/Kyiv">link</a></p> # should be changed
        """
    iframe = folium.IFrame(html=html, width=200, height=200)
    popup = folium.Popup(iframe, max_width=2650)
    folium.Marker(
        location=[data.iloc[i]['lat'], data.iloc[i]['lon']],
        popup=popup,
        icon=folium.DivIcon(html=f"""
            <div><svg>
                <circle cx="50" cy="50" r="40" fill="#69b3a2" opacity=".4"/>
                <rect x="35", y="35" width="30" height="30", fill="red", opacity=".3" 
            </svg></div>""")
    ).add_to(n)

# Show the map again
n

You can see the scripts on Google Colaboratory here:

https://colab.research.google.com/drive/17CaURvPdPt6npzsmV9Fz9YZzzkDNb3n_?usp=sharing

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.