Wednesday, 7 August 2024

Create a map using Python

 

pip install folium

import folium
from IPython.display import display

map_center = [40.7128, -74.0060]
mymap = folium.Map(location=map_center, zoom_start=12)

folium.Marker(
    [40.7128, -74.0060],  
    popup="New York", 
    icon=folium.Icon(color="blue", icon="info-sign")
).add_to(mymap)

display(mymap)

The above code is used to create and display an interactive map using the folium library in Python, specifically within a Jupyter Notebook environment. Below is a step-by-step explanation of the code:

1. Install the folium library

pip install folium
  • pip install folium: This command installs the folium library, which is used for creating interactive maps with Leaflet.js, a popular JavaScript library for mapping.

2. Import necessary libraries

import folium
from IPython.display import display
  • import folium: This imports the folium library, which allows you to create maps and add various layers and markers to them.
  • from IPython.display import display: This imports the display function from IPython.display, enabling the map to be rendered directly within a Jupyter Notebook.

3. Define the center of the map

map_center = [40.7128, -74.0060]
  • map_center: This variable defines the latitude and longitude coordinates of the center of the map, which in this case is set to New York City (latitude: 40.7128, longitude: -74.0060).

4. Create a folium map object

mymap = folium.Map(location=map_center, zoom_start=12)
  • folium.Map(location=map_center, zoom_start=12): This creates a map centered at the specified location with a zoom level of 12, which gives a city-level view of New York.

5. Add a marker to the map

folium.Marker( 
    [40.7128, -74.0060], 
    popup="New York", 
    icon=folium.Icon(color="blue", icon="info-sign") 
  ).add_to(mymap)
  • folium.Marker([40.7128, -74.0060], popup="New York", 
  • icon=folium.Icon(color="blue", icon="info-sign")): This adds a marker to the map at the specified coordinates (New York City). The marker has a popup label "New York" that appears when the marker is clicked, and the icon is styled with a blue color and an info-sign symbol.
  • .add_to(mymap): This adds the marker to the mymap object.

6. Display the map

display(mymap)
    display(mymap): This displays the map in the Jupyter Notebook.

Summary

This code creates an interactive map centered on New York City, adds a marker with a popup and a custom icon, and displays the map within a Jupyter Notebook.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (3) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (205) Cybersecurity (24) data management (11) Data Science (109) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (27) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (50) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (898) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses