Python folium example

Louis 93 Published: 11/10/2024

Python folium example

I'll respond in English, and I'll provide a Python Folium example for you.

Folium is a powerful open-source library that allows you to create interactive maps using the popular Python programming language. With Folium, you can create a wide range of map visualizations, from simple maps with markers or polygons to complex interactive dashboards with real-time data updates.

Here's an example of how you can use Folium to create an interactive map:

import folium

from IPython.display import IFrame

Create a new map instance

m = folium.Map(location=[40, -100], zoom_start=4)

Add markers for major cities in the United States

for city in ['New York', 'Los Angeles', 'Chicago', 'Houston']:

if city == 'New York':

folium.Marker([40.7128, 74.0060],

popup='The Big Apple!').add_to(m)

elif city == 'Los Angeles':

folium.Marker([34.0522, 118.2437],

popup='La-La Land!').add_to(m)

elif city == 'Chicago':

folium.Marker([41.8781, 87.6298],

popup='The Windy City!').add_to(m)

else:

folium.Marker([29.7633, 95.3632],

popup='Space City!').add_to(m)

Add a polygon for the state of California

folium.RegularPolygonMarker(location=[37.7749, -122.4194],

radius=100000,

tooltip='California!',

fill_color='blue',

fill_opacity=0.5).add_to(m)

Display the map

m

IFrame('map.html', width=800, height=600)

In this example, we create a new Folium map instance and set its location to the center of the United States (40° north latitude and 100° west longitude) with an initial zoom level of 4. We then add markers for major cities in the United States using the folium.Marker class.

Next, we add a polygon marker representing the state of California using the folium.RegularPolygonMarker class. Finally, we display the map using the IFrame class from IPython.

When you run this code, Folium will generate an interactive HTML file that you can open in a web browser to see the map in action. You can zoom in and out, pan across the map, and click on markers and polygons to get more information about each location.

I hope this helps! Let me know if you have any questions or need further assistance.

folium python examples

I'm happy to help!

Folium is a Python library used for creating interactive maps with Plotly. Here are some examples:

Example 1: Basic Map

import folium

map = folium.Map(location=[45, -70], zoom_start=4)

map

This code creates a basic map centered at the location (45, -70) with a initial zoom level of 4. The map is not saved to a file; instead, it's displayed directly in your web browser.

Example 2: Adding Markers

import folium

map = folium.Map(location=[45, -70], zoom_start=4)

marker1 = folium.Marker([40, -75], popup='Hello', icon=folium.Icon(color='red'))

marker2 = folium.Marker([50, -60], popup='Hi', icon=folium.Icon(color='blue'))

map.add_child(marker1)

map.add_child(marker2)

map

This code creates two markers on the map. The markers are displayed with red and blue icons, respectively.

Example 3: Adding Pop-Up Text

import folium

map = folium.Map(location=[45, -70], zoom_start=4)

marker1 = folium.Marker([40, -75])

marker1.add_child(folium.Popup('Hello from marker 1'))

marker2 = folium.Marker([50, -60])

marker2.add_child(folium.Popup('Hi from marker 2'))

map.add_child(marker1)

map.add_child(marker2)

map

This code creates two markers on the map with pop-up text. When you hover over the markers, the pop-up text appears.

Example 4: Adding a GeoJSON Layer

import folium

m = folium.Map(location=[45, -70], zoom_start=4)

folium.GeoJson('data.geojson').add_to(m)

m

This code creates a map with a layer from a GeoJSON file. The GeoJSON data is loaded from the 'data.geojson' file and displayed on the map.

Example 5: Creating an Image Overlay

import folium

map = folium.Map(location=[45, -70], zoom_start=4)

overlay = folium.rasterize('image.png', opacity=0.5).add_to(map)

map

This code creates a map with an image overlay. The 'image.png' file is rasterized and displayed on the map with an opacity of 0.5.

These are just a few examples of what you can do with Folium. I hope this helps!