8.3.1.Advanced Visualization Tools & Visualizing Geospatial Data - sj50179/IBM-Data-Science-Professional-Certificate GitHub Wiki
Advanced Visualization Tools
Seaborn and Regression Plots
Seaborn
- Seaborn is a a Python visualization library based on Matplotlib.
- Visuals that need ~20 lines of code using Matplotlib to be created, with seaborn, the number of lines of code is reduced by 5-fold.
Regression Plots
- Example:
import seaborn as sns
ax = sns.regplot(x='year', y='total', data=df_total, color='green', marker='+')
Advanced Visualization Tools
LATEST SUBMISSION GRADE 100%
Question 1
Seaborn is a Python visualization library that provides a high-level interface for drawing attractive statistical graphics, such as regression plots and box plots.
- True
False
Correct.
Question 2
The following code
import seaborn as sns
ax = sns.regplot(x="year", y="total", data=data_df, color="green", marker="+")
creates the following regression plot.
- True
False
Correct.
Question 3
In Python, creating a waffle chart is straightforward since we can easily create one using the scripting layer of Matplotlib.
True- False
Correct.
Visualizing Geospatial Data
Introduction to Folium
What is Folium?
- Folium is a powerful Python library that helps you create several types of Leaflet maps.
- It enables both the binding of data to a map for choropleth visualizations as well as passing visualizations as markers on the map.
- The library has a number of bulit-in tilesets from OpenStreetMap, Mapbox, and Stamen, and supports custom tilesets with Mapbox API keys.
Creating a World Map
# define the world map
world_map = folium.Map()
# display world map
world_map
Creating a Map of Canada
# define the world map centered around
# Canada with a low zoom level
world_map = folium.Map(
location=[56.130, -106.35],
zoom_start=4
)
# dispaly world map
world_map
Map Styles - Stamen Toner
# create a Stamen Toner map of
# the world centered around Canada
world_map = folium.Map(
location=[56.130, -106.35],
zoom_start=4,
titles='Stamen Toner'
)
# dispaly map
world_map
Map Styles - Stamen Terrain
# create a Stamen Toner map of
# the world centered around Canada
world_map = folium.Map(
location=[56.130, -106.35],
zoom_start=4,
titles='Stamen Terrain'
)
# dispaly map
world_map
Maps with Markers
Label the Marker
# generate map of Canada
canada_map = folium.Map(
location=[56.130, -106.35],
zoom_start=4
)
## add a red marker to Ontario
# create a feature group
ontario = folium.map.FeatureGroup()
# style the feature group
ontario.add_child(
folium.features.CircleMarker(
[51.25, -85.32], radium=5,
color='red', fill_color='Red'
)
)
# add the feature group to the map
canada_map.add_child(ontario)
# label the marker
folium.Marker([51.25, -85.32],
popup='Ontario').add_to(canada_map)
# display map
canada_map
Choropleth Maps
Choropleth Maps
Geojson File
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"name": "Brunei"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[114.204017, 4.525874], [114.599961, 4.900011], [115.45071, 5.44773],
[115.4057, 4.955228], [115.347461, 4.316636], [114.869557, 4.348314],
[114.659596, 4.007637], [114.204017, 4.525874]
]
]
},
"id": "BRN"
},
Creating the Map
# create a plain world map
world_map = folium.Map(
zoom_start=2,
title="Mapbox Bright'
)
## geojson file
world_geo = r'world_cointries.json'
# generate choropleth map using the total
# population of each country to Canada from
# 1980 to 2013
world_map.choropleth(
geo_path=world_geo,
data=df_canada,
columns=['Country', 'Total'],
key_on='feature.properties.name',
fill_color='YlOrRd',
legend_name='Immigration to Canada'
)
# display map
world_map
Visualizing Geospatial Data
LATEST SUBMISSION GRADE 100%
Question 1
You cluster markers, superimposed onto a map in Folium, using a marker cluster object.
- True
False
Correct.
Question 2
The following code will generate a map of Spain, displaying its hill shading and natural vegetation.
folium.Map(location=[40.4637, -3.7492], zoom_start=6, tiles='Stamen Terrain')
- True
False
Correct.
Question 3
A choropleth map is a thematic map in which areas are shaded or patterned in proportion to the measurement of the statistical variable being displayed on the map.
- True
False
Correct.