Finance[US] Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
3.5k views
in Python Programming by Goeduhub's Expert (3.1k points)

Folium Library in Python (Interactive Maps in Python). What is Folium in Python? How do I import Folium into Python? How do you use Folium in Jupyter notebook? What are tile styles of Folium maps? How do you make a Boxplot in Python? How do you make a Choropleth map in Python?

2 Answers

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

What is Folium in Python?

Folium is a Python Library that allow us to visualize spatial data (geographical data) in an interactive manner (you can drag,zoom,click on the map ).Manipulate your data in Python, then visualize it in on a Leaflet map via folium.

Official Documentation of Folium (Click Here)

Geographical Data: All kinds of data that contain geographical (latitude, longitude, altitude, shape) as part of its feature. (Maps!).

Note that to see the position of any place (place in city,city,district, state,country etc...) on the map, we need latitude and longitude.

Here is a link to a site that I follow to find the position (latitude and longitude) of any place (click here).

Leaflet Map: Leaflet is the leading open-source JavaScript library (leaflet.js) for mobile-friendly interactive maps.It has all the mapping features most developers ever need.

Installation of folium

Installation is same as other libraries in python 

$ pip install folium

Or

How do you use(Install) Folium in Jupyter Notebook or Anaconda?

$ conda install folium -c conda-forge

What are tile styles of Folium maps?

Before getting started let's understand what is tiles and zooming in map 

Tiles are 256x256 pixels.At the outer most zoom level, 0, the entire world can be rendered in a single map tile (consider world map as one tile).Each zoom level doubles in both dimensions, so a single tile is replaced by 4 tiles when zooming in.

This means that about 22 zoom levels are sufficient for most practical purposes. (You can also see it by doing it on Google map, try it just open your google map start with maximum zoom and see the map after 22 zooms.)

Most tiled web maps follow certain Google Maps conventions.

For clarification see the image below where you can see that how tiles and zooms work in a map.

zooming in maps

Getting Started

To create a basic map, simply pass your starting coordinates (longitude and latitude) to Folium map function.

#Creating simple map

marker=folium.Map(location=[28.704060,77.102493],

           zoom_start=4)

marker

#saving a map in folium (create a html page)

marker.save('index.html') 

Output

India basic map

Note 

We have not done much here, just pass the position/location (longitude and latitude)  in the folium map function and we got our map. We can save the map in Folium. The map will be saved in the form of html file (that you can use later).

#styling of map

mapp=  folium.Map(location=[26.920980,75.794220],zoom_start=4,tiles="Stamen Terrain")

mapp

Output

styling of map

Note 

The default tiles are set to OpenStreetMap in folium library but Stamen Terrain, Stamen Toner , Mapbox Bright and Mapbox Control Room and many other tiles are built in , in folium.

To get any of these tiles (map style) you just have to pass a parameter tiles in folium map function as we did in above code.

Markers and Popup 

There are numerous marker types (see the google map), start with simple style location marker with popup.

#creating a simple marker 

folium.Marker([28.609140,77.234138], popup='India Gate', tooltip="click me for more").add_to(marker)

marker

Output

marker

Note: As you can see from the output we have created a simple marker on location of India gate Delhi with popup message 'India Gate'.

Creating different markers and icons in map

#Different types of markers and icon 

marker=folium.Map(location=[28.704060,77.102493],

           zoom_start=10)

#adding blue circle marker to india gate

folium.CircleMarker(

    [28.609140,77.234138],

    radius=8,

    popup='India Gate',

    color='#3186cc',

    fill=True,

    fill_color='#3186cc'

).add_to(marker)

#adding red circle marker to red fort 

folium.CircleMarker(

    location=[28.6562,77.2410],

    radius=10,

    popup='red Fort',

    color='#ff0033',

    fill=True,

).add_to(marker)

#adding icon to the map 

folium.Marker(

    [28.6304,77.2177],

    popup='CP Delhi',

    icon=folium.Icon(color='#ff0033', icon='fas fa-shopping-cart')

).add_to(marker)

marker

Output

icon and markers 

Note

As you can see in the output, we have made three markers here.

The first marker, which is a circle marker, is used in red color for the red fort.

We used the second marker in blue color circle for India Gate. 

And the third marker in which we have shown a shopping cart icon, used for the cp (Connaught Place, shopping mart) Delhi .

When you people use Google Map, then you must have seen many icons popping location name.Here is a link of listed icons (click here).

Plotting Geojson/ json and Topojson file on map 

Geojson/json files: GeoJSON is a JSON (JavaScript Object Notation) based format designed to represent the geographical features with their non-spatial attributes.The features reflect addresses and places as point’s streets, main roads and borders as line strings and countries, provinces, and land regions as polygons.

TopoJSON: An extension of GeoJSON is TopoJSON that is smaller in size and encodes geospatial topology.

Here we used Indian states json file to define the state boundaries and multiple layers can be visualized on the same map. Link of Indian states Geo json file ( click here ).used here.

#plotting Geojson data on map

marker=folium.Map(location=[28.704060,77.102493],zoom_start=4)

folium.GeoJson('india_states.json').add_to(marker)

marker

Output

Note: Here we just Indian state Geojson file to define Indian state boundaries.


Python Tutorial 

Machine Learning Tutorial 

AI Tutorial

0 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by

Choropleth maps

A choropleth map is a type of thematic map in which areas are shaded or patterned in proportion to a statistical variable that represents an aggregate summary of a geographic characteristic within each area, such as population density or per-capita income.

Note: We had defined the boundaries (and other feature also) from the json file, if we add any relevant data (statistical data) to the json file, then we can easily visualize the data on the map.

How do you make a Choropleth map in Python?

Example: In this example we are going to plot coronavirus cases in India with sates/UT.

#coronavirus cases in india 

import pandas as pd

#reading indian state json file 

state_geo = r'india_states.json'

state_data =pd.read_csv("cases.csv")

print(state_data.head())

m = folium.Map(location=[20.593683, 78.962883], zoom_start=4)

#Defining a choropleth map

folium.Choropleth(

    geo_data=state_geo,

    name='choropleth',

    data=state_data,

    columns=['Name of State / UT', 'Total Confirmed cases (Indian National)'],

    key_on='feature.properties.NAME_1',

    fill_color='YlGn',

    fill_opacity=0.7,

    line_opacity=0.8,

    legend_name='cases in india'

).add_to(m)

folium.LayerControl().add_to(m)

m

Output

Note 

Here we plotted coronavirus Confirmed data on map with state wise.You must have a question: Why is the color of some states dark?

Answer is simpleThe names of states do not match in both the files (Indian state json file and cases file).So nothing happened in them.

Here i am not providing CSV data used here you can make it using excel. 

What are tile styles of Folium maps?

Styling of maps (ways of using tiles) *just extra information if you want to learn more.

#map layer control (controlling different layers in map)

map_layer_control=marker=folium.Map(location=[28.704060,77.102493],

           zoom_start=4)

#Adding different layers

# adding openstreetlayer map

folium.raster_layers.TileLayer

('OpenStreetMap',attr="map").add_to(map_layer_control)

# adding Stamen Terrain map

folium.raster_layers.TileLayer

('Stamen Terrain',attr="map").add_to(map_layer_control)

# adding Stamen Toner map

folium.raster_layers.TileLayer

('Stamen Toner',attr="map").add_to(map_layer_control)

# adding CartoDB Positron layer map

folium.raster_layers.TileLayer

('CartoDB Positron',attr="map").add_to(map_layer_control)

#Applying layer control function of folium 

folium.LayerControl().add_to(map_layer_control)

map_layer_control

Output

layers map

Note 

Here we just added different layers/ tiles / style in one map. And in a corner (red circle) a list is created with all the tiles/layers we have mentioned in code. When we change the layer, the view of our map will also be changed.

How do you make a Boxplot in Python?

More Ways to ADD maps layers/style/ maps etc.. 

You can also use the mapbox to style the map (Mapbox official site). here is a practical implementation of how to use mapbox (click here to see).

By using your API key provided by mapbox or cloudmade you can use map or map style.

#maps using mapbox

folium.Map(location=[45.5236, -122.6750],

           tiles='Mapbox',

           API_key='your.API.key')

Another way is you can use leaflet.js compatible custom tileset. For more information about it see the link (click here).

#map tiles using leaflet.js files 

folium.Map(location=[45.372, -121.6972],

           zoom_start=12,

      tiles='http://{s}.tiles.yourtiles.com/{z}/{x}/{y}.png',

           attr='My Data Attribution')

Images are served through a Web server, with a URL like , where Z is the zoom level, and X and Y identify the tile.


Artificial Intelligence(AI) Training in Jaipur 

Machine Learning(ML) Training in Jaipur 

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 1.0k views
asked May 14, 2020 in Python Programming by Nisha Goeduhub's Expert (3.1k points)
0 like 0 dislike
1 answer 1.5k views
0 like 0 dislike
2 answers 1.4k views
asked Sep 14, 2020 in Python Programming by Nisha Goeduhub's Expert (3.1k points)

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...