Visualizing geospatial data#

Generating maps from data is a nice way to generate additional insight into data and make it instantly accessible and understandable.

Visualizing geospatial data is a powerful tool for gaining insights and understanding patterns in data.

By mapping data onto a geographic space, it is possible to uncover relationships and trends that might otherwise be difficult to discern. Whether tracking the spread of a virus across the globe, identifying traffic jams in a city, or analyzing consumer behaviour across different regions, geospatial visualization can reveal patterns and relationships that might not be immediately apparent from raw data alone.

With advances in technology and data analytics, creating maps and visualizations has become easier and more accessible than ever, making it a valuable tool for researchers, policymakers, and businesses.

How To#

import pandas as pd

df = pd.read_csv("data/housing.csv")
df.head()
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value ocean_proximity
0 -122.23 37.88 41.0 880.0 129.0 322.0 126.0 8.3252 452600.0 NEAR BAY
1 -122.22 37.86 21.0 7099.0 1106.0 2401.0 1138.0 8.3014 358500.0 NEAR BAY
2 -122.24 37.85 52.0 1467.0 190.0 496.0 177.0 7.2574 352100.0 NEAR BAY
3 -122.25 37.85 52.0 1274.0 235.0 558.0 219.0 5.6431 341300.0 NEAR BAY
4 -122.25 37.85 52.0 1627.0 280.0 565.0 259.0 3.8462 342200.0 NEAR BAY
import folium
m = folium.Map(location=[df.latitude.mean(), df.longitude.mean()])
m
Make this Notebook Trusted to load map: File -> Trust Notebook
m = folium.Map(location=[df.latitude.mean(), df.longitude.mean()], zoom_start=6)

for i, row in df.sample(15).iterrows():
    folium.Marker([row.latitude, row.longitude], tooltip=row.ocean_proximity).add_to(m)
m
Make this Notebook Trusted to load map: File -> Trust Notebook
from folium import plugins
heatmap = folium.Map(location=[df.latitude.mean(), df.longitude.mean()], zoom_start=6, tiles='Stamen Toner',)

# plot heatmap
heatmap.add_children(folium.plugins.HeatMap(df[['latitude', 'longitude']].values, radius=15))
heatmap
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[5], line 2
      1 from folium import plugins
----> 2 heatmap = folium.Map(location=[df.latitude.mean(), df.longitude.mean()], zoom_start=6, tiles='Stamen Toner',)
      4 # plot heatmap
      5 heatmap.add_children(folium.plugins.HeatMap(df[['latitude', 'longitude']].values, radius=15))

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/folium/folium.py:302, in Map.__init__(self, location, width, height, left, top, position, tiles, attr, min_zoom, max_zoom, zoom_start, min_lat, max_lat, min_lon, max_lon, max_bounds, crs, control_scale, prefer_canvas, no_touch, disable_3d, png_enabled, zoom_control, **kwargs)
    300     self.add_child(tiles)
    301 elif tiles:
--> 302     tile_layer = TileLayer(
    303         tiles=tiles, attr=attr, min_zoom=min_zoom, max_zoom=max_zoom
    304     )
    305     self.add_child(tile_layer, name=tile_layer.tile_name)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/folium/raster_layers.py:139, in TileLayer.__init__(self, tiles, min_zoom, max_zoom, max_native_zoom, attr, detect_retina, name, overlay, control, show, no_wrap, subdomains, tms, opacity, **kwargs)
    137 self.tiles = tiles
    138 if not attr:
--> 139     raise ValueError("Custom tiles must have an attribution.")
    141 self.options = parse_options(
    142     min_zoom=min_zoom,
    143     max_zoom=max_zoom,
   (...)
    151     **kwargs,
    152 )

ValueError: Custom tiles must have an attribution.
folium.Choropleth(
    
)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 folium.Choropleth(
      2     
      3 )

TypeError: __init__() missing 1 required positional argument: 'geo_data'

Exercise#

Change the basemap and chose different marker types.

Additional Resources#