{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapping & Geocoding" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this lesson, we're going to learn how to analyze and visualize geographic data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geocoding with GeoPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we're going to geocode data — aka get coordinates from addresses or place names — with the Python package [GeoPy](https://geopy.readthedocs.io/en/stable/#). GeoPy makes it easier to use a range of third-party [geocoding API services](https://geopy.readthedocs.io/en/stable/#), such as Google, Bing, ArcGIS, and OpenStreetMap.\n", "\n", "Though most of these services require an API key, Nominatim, which uses OpenStreetMap data, does not, which is why we're going to use it here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Install GeoPy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install geopy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Nominatim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From GeoPy's list of possible geocoding services, we're going to import Nominatim:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from geopy.geocoders import Nominatim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nominatim & OpenStreetMap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nominatim (which means \"name\" in Latin) uses [OpenStreetMap data](https://www.openstreetmap.org/relation/174979) to match addresses with geopgraphic coordinates. Though we don't need an API key to use Nominatim, we do need to create a unique [application name](https://operations.osmfoundation.org/policies/nominatim/). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we're initializing Nominatim as a variable called `geolocator`. Change the application name below to your own application name:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "geolocator = Nominatim(user_agent=\"YOUR NAME's mapping app\", timeout=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To geocode an address or location, we simply use the `.geocode()` function:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "location = geolocator.geocode(\"South Cayuga Street\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Location(South Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America, (42.4359281, -76.4988639, 0.0))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "location" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Google Geocoding API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Google Geocoding API is superior to Nominatim, but it requires an API key and more set up. To enable the Google Geocoding API and get an API key, see [Get Started with Google Maps Platform](https://developers.google.com/maps/gmp-get-started) and [Get Started with Geocoding API](https://developers.google.com/maps/documentation/geocoding/start)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "hide-input" ] }, "outputs": [], "source": [ "#from geopy.geocoders import GoogleV3\n", "#google_geolocator = GoogleV3(api_key=\"YOUR-API-KEY HERE\")\n", "#google_geolocator.geocode(\"Cayuga Street\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get Address" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "South Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America\n" ] } ], "source": [ "print(location.address)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get Latitude and Longitude" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42.4359281 -76.4988639\n" ] } ], "source": [ "print(location.latitude, location.longitude)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get \"Importance\" Score" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Importance: 0.4\n" ] } ], "source": [ "print(f\"Importance: {location.raw['importance']}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get Class and Type" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Class: highway \n", "Type: residential\n" ] } ], "source": [ "print(f\"Class: {location.raw['class']} \\nType: {location.raw['type']}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get Multiple Possible Matches" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true, "tags": [ "output_scroll" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "College Avenue, Pemberton Tract, Midland, Midland County, Texas, United States of America\n", "31.99450735 -102.08441405117188\n", "Importance: 0.35000000000000003\n", "College Avenue, Illinois Prairie Path, Wheaton, DuPage County, Illinois, 60187, United States of America\n", "41.8683333 -88.0902778\n", "Importance: 0.34778803910650136\n", "College Avenue, Grantham, Cumberland, Pennsylvania, 17027, United States of America\n", "40.155326 -76.994622\n", "Importance: 0.3\n", "College Avenue, London, Southwestern Ontario, Ontario, N6A 3H1, Canada\n", "42.9982465 -81.2561233\n", "Importance: 0.3\n", "College Avenue, Spring Valley Estates, Athens-Clarke County Unified Government, Athens-Clarke County, Georgia, 30334-1458, United States of America\n", "33.9578193 -83.3752778\n", "Importance: 0.3\n", "College Avenue, Windsor, Southwestern Ontario, Ontario, N9C 2K7, Canada\n", "42.298467 -83.0652786\n", "Importance: 0.3\n", "College Avenue, Uptown, Racine, Wisconsin, 53403, United States of America\n", "42.7160925 -87.784789\n", "Importance: 0.3\n", "College Avenue, Morris, Stevens County, Minnesota, 56267, United States of America\n", "45.5906183 -95.9045979\n", "Importance: 0.3\n", "College Avenue, Swarthmore, Delaware County, Pennsylvania, 19081, United States of America\n", "39.9058529 -75.3474084\n", "Importance: 0.3\n", "College Avenue, South Woodstock, Woodstock, Oxford County, Southwestern Ontario, Ontario, N4S 2E3, Canada\n", "43.1246427 -80.745315\n", "Importance: 0.3\n" ] } ], "source": [ "possible_locations = geolocator.geocode(\"College Ave\", exactly_one=False)\n", "\n", "for location in possible_locations:\n", " print(location.address)\n", " print(location.latitude, location.longitude)\n", " print(f\"Importance: {location.raw['importance']}\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "College Avenue, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America\n", "42.4431445 -76.4852319\n", "Importance: 0.41000000000000003\n" ] } ], "source": [ "location = geolocator.geocode(\"College Ave, Ithaca NY\")\n", "\n", "print(location.address)\n", "print(location.latitude, location.longitude)\n", "print(f\"Importance: {location.raw['importance']}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geocode with Pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To geocode every location in a CSV file, we can use Pandas, make a Python function, and `.apply()` it to every row in the CSV file." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "pd.set_option(\"max_rows\", 400)\n", "pd.set_option(\"max_colwidth\", 400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{admonition} Pandas\n", ":class: pandasreview\n", " Do you need a refresher or introduction to the Python data analysis library Pandas? Be sure to check out Pandas Basics (1-3) in this textbook!\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we make a function with `geolocator.geocode()` and ask it to return the address, lat/lon, and importance score:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def find_location(row):\n", " \n", " place = row['place']\n", " \n", " location = geolocator.geocode(place)\n", " \n", " if location != None:\n", " return location.address, location.latitude, location.longitude, location.raw['importance']\n", " else:\n", " return \"Not Found\", \"Not Found\", \"Not Found\", \"Not Found\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To start exploring, let's read in a CSV file with a list of places in and around Ithaca." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "ithaca_df = pd.read_csv(\"../data/ithaca-places.csv\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
place
0College Town Bagels
1Ithaca Falls
2Moosewood Restaurant
3Cascadilla Gorge Trail
4Phillips Hall
5Carriage House Cafe
6Olin Library
7Purity Ice Cream
8Buttermilk Falls State Park
9Libe Slope
10Mundy Wildflower Garden
11Gimme! Coffee
12Beebe Lake
13Ithaca Tompkins Airport
14Wharton Studio Inc.
\n", "
" ], "text/plain": [ " place\n", "0 College Town Bagels\n", "1 Ithaca Falls\n", "2 Moosewood Restaurant\n", "3 Cascadilla Gorge Trail\n", "4 Phillips Hall\n", "5 Carriage House Cafe\n", "6 Olin Library\n", "7 Purity Ice Cream\n", "8 Buttermilk Falls State Park\n", "9 Libe Slope\n", "10 Mundy Wildflower Garden\n", "11 Gimme! Coffee\n", "12 Beebe Lake\n", "13 Ithaca Tompkins Airport\n", "14 Wharton Studio Inc." ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ithaca_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's `.apply()` our function to this Pandas dataframe and see what results Nominatim's geocoding service spits out." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
placeaddresslatlonimportance
0College Town BagelsCollegetown Bagels, College Avenue, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America42.4424-76.48510.35
1Ithaca FallsIthaca Falls, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America42.4528-76.49160.48084
2Moosewood RestaurantMoosewood Restaurant, North Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America42.4406-76.49860.475949
3Cascadilla Gorge TrailCascadilla Gorge Trail, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America42.4429-76.48210.375
4Phillips HallPhillips Hall, Oak Street, Oxford, Oxford Township, Butler County, Ohio, 45056, United States of America39.5052-84.73680.201
5Carriage House CafeCarriage House Cafe, B4380, Atcham, Shropshire, West Midlands, England, SY5 6QQ, United Kingdom52.6857-2.669690.301
6Olin LibraryOlin Library, Holt Avenue, Winter Park, Orange County, Florida, 32789, United States of America28.591-81.34920.201
7Purity Ice CreamPurity Ice Cream, Cascadilla Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America42.4445-76.50890.301
8Buttermilk Falls State ParkButtermilk Falls State Park, South Hill, Ithaca Town, Tompkins County, New York, United States of America42.4162-76.51640.771981
9Libe SlopeLibe Slope, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, United States of America42.4484-76.48660.4
10Mundy Wildflower GardenMundy Wildflower Garden, Forest Home, Ithaca Town, Tompkins County, New York, United States of America42.4505-76.46920.45
11Gimme! CoffeeGimme! Coffee, West State Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America42.4395-76.50640.201
12Beebe LakeBeebe Lake, Forest Home Drive, Forest Home, Ithaca Town, Tompkins County, New York, 14853, United States of America42.4512-76.47570.201
13Ithaca Tompkins AirportIthaca Tompkins Regional Airport, Brown Road, Lansing, Lansing Town, Tompkins County, New York, 14850, United States of America42.491-76.45870.624671
14Wharton Studio Inc.Not FoundNot FoundNot FoundNot Found
\n", "
" ], "text/plain": [ " place \\\n", "0 College Town Bagels \n", "1 Ithaca Falls \n", "2 Moosewood Restaurant \n", "3 Cascadilla Gorge Trail \n", "4 Phillips Hall \n", "5 Carriage House Cafe \n", "6 Olin Library \n", "7 Purity Ice Cream \n", "8 Buttermilk Falls State Park \n", "9 Libe Slope \n", "10 Mundy Wildflower Garden \n", "11 Gimme! Coffee \n", "12 Beebe Lake \n", "13 Ithaca Tompkins Airport \n", "14 Wharton Studio Inc. \n", "\n", " address \\\n", "0 Collegetown Bagels, College Avenue, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America \n", "1 Ithaca Falls, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America \n", "2 Moosewood Restaurant, North Cayuga Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14850, United States of America \n", "3 Cascadilla Gorge Trail, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, 14853, United States of America \n", "4 Phillips Hall, Oak Street, Oxford, Oxford Township, Butler County, Ohio, 45056, United States of America \n", "5 Carriage House Cafe, B4380, Atcham, Shropshire, West Midlands, England, SY5 6QQ, United Kingdom \n", "6 Olin Library, Holt Avenue, Winter Park, Orange County, Florida, 32789, United States of America \n", "7 Purity Ice Cream, Cascadilla Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America \n", "8 Buttermilk Falls State Park, South Hill, Ithaca Town, Tompkins County, New York, United States of America \n", "9 Libe Slope, East Ithaca, Ithaca, Ithaca Town, Tompkins County, New York, United States of America \n", "10 Mundy Wildflower Garden, Forest Home, Ithaca Town, Tompkins County, New York, United States of America \n", "11 Gimme! Coffee, West State Street, South Hill, Ithaca, Ithaca Town, Tompkins County, New York, 14851, United States of America \n", "12 Beebe Lake, Forest Home Drive, Forest Home, Ithaca Town, Tompkins County, New York, 14853, United States of America \n", "13 Ithaca Tompkins Regional Airport, Brown Road, Lansing, Lansing Town, Tompkins County, New York, 14850, United States of America \n", "14 Not Found \n", "\n", " lat lon importance \n", "0 42.4424 -76.4851 0.35 \n", "1 42.4528 -76.4916 0.48084 \n", "2 42.4406 -76.4986 0.475949 \n", "3 42.4429 -76.4821 0.375 \n", "4 39.5052 -84.7368 0.201 \n", "5 52.6857 -2.66969 0.301 \n", "6 28.591 -81.3492 0.201 \n", "7 42.4445 -76.5089 0.301 \n", "8 42.4162 -76.5164 0.771981 \n", "9 42.4484 -76.4866 0.4 \n", "10 42.4505 -76.4692 0.45 \n", "11 42.4395 -76.5064 0.201 \n", "12 42.4512 -76.4757 0.201 \n", "13 42.491 -76.4587 0.624671 \n", "14 Not Found Not Found Not Found " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ithaca_df[['address', 'lat', 'lon', 'importance']] = ithaca_df.apply(find_location, axis=\"columns\", result_type=\"expand\")\n", "ithaca_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**What do you notice about these results?** ☝️☝️☝️" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[Wharton Studio Inc.](https://exhibits.library.cornell.edu/biggest-little-fashion-city/feature/wharton-studio-inc)** (1914-1919) — early 20th-century Ithaca movie studio, located in what is now Stewart Park \n", "*To check out more historical photos of Wharton Studio Inc., see [the Cornell library](https://digital.library.cornell.edu/catalog/ss:550440).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Making Interactive Maps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To map our geocoded coordinates, we're going to use the Python library [Folium](https://python-visualization.github.io/folium/). Folium is built on top of the popular JavaScript library [Leaflet](https://leafletjs.com/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To install and import Folium, run the cells below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "output_scroll" ] }, "outputs": [], "source": [ "!pip install folium" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "import folium" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Base Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we need to establish a base map. This is where we'll map our geocoded Ithaca locations. To do so, we're going to call `folium.Map()`and enter the general latitude/longitude coordinates of the Ithaca area at a particular zoom.\n", "\n", "(To find latitude/longitude coordintes for a particular location, you can use Google Maps, [as described here](https://support.google.com/maps/answer/18539?co=GENIE.Platform%3DDesktop&hl=en).)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ithaca_map = folium.Map(location=[42.44, -76.5], zoom_start=14)\n", "ithaca_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a Marker" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding a marker to a map is easy with Folium! We'll simply call `folium.Marker()` at a particular lat/lon, enter some text to display when the marker is clicked on, and then add it to our base map." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "folium.Marker(location=[42.444695, -76.482233], popup=\"Intro to Cultural Analytics\").add_to(ithaca_map)\n", "ithaca_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add Markers From Pandas Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To add markers for every location in our Pandas dataframe, we can make a Python function and `.apply()` it to every row in the dataframe." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def create_map_markers(row, map_name):\n", " folium.Marker(location=[row['lat'], row['lon']], popup=row['place']).add_to(map_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before we apply this function to our dataframe, we're going to drop any locations that were \"Not Found\" (which would cause `folium.Marker()` to return an error)." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "found_ithaca_locations = ithaca_df[ithaca_df['address'] != \"Not Found\"]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "found_ithaca_locations.apply(create_map_markers, map_name=ithaca_map, axis='columns')\n", "ithaca_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save Map" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "ithaca_map.save(\"Ithaca-map.html\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Torn Apart / Separados" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data in this section was drawn from [Torn Apart / Separados Project](https://github.com/xpmethod/torn-apart-open-data). It maps the locations of Immigration and Customs Enforcement (ICE) detention facilities, as featured in [Volume 1](http://xpmethod.plaintext.in/torn-apart/volume/1/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a Circle Marker" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a few [different kinds of markers](https://python-visualization.github.io/folium/quickstart.html#Markers) that we can add to a Folium map, including circles. To make a circle, we can call `folium.CircleMarker()` with a particular radius and the option to fill in the circle. You can explore more customization options in the [Folium documentation](https://python-visualization.github.io/folium/modules.html#folium.vector_layers.CircleMarker). We're also going to add a hover `tooltip` in addition to a `popup`." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "def create_ICE_map_markers(row, map_name):\n", " \n", " folium.CircleMarker(location=[row['lat'], row['lon']], raidus=100, fill=True,\n", " popup=folium.Popup(f\"{row['Name'].title()}
{row['City'].title()}, {row['State']}\", max_width=200),\n", " tooltip=f\"{row['Name'].title()}
{row['City'].title()}, {row['State']}\"\n", " ).add_to(map_name)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "scrolled": true, "tags": [ "output_scroll" ] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
latlonadpSumonWebFlagsfulladdrDETLOCNameAddressCity...ICE.Threat.Level.2ICE.Threat.Level.3No.ICE.Threat.LevelFacility.OperatorFY17.Calendar.Days.in.UseFY17...of.Days.in.UseFY17.Total.MandaysFY17.Max.Pop.Countgeocodelatgeocodelon
028.895000-99.121200839128.8950NaN566 VETERANS DRIVE PEARSALL TX 78061STCDFTXSOUTH TEXAS DETENTION COMPLEX566 VETERANS DRIVEPEARSALL...1123111187GEO3721.02598554185428.896498-99.116863
132.036600-84.771800800432.0366NaN146 CCA ROAD LUMPKIN GA 31815STWRTGASTEWART DETENTION CENTER146 CCA ROADLUMPKIN...344365671CCA3721.02671515199232.037982-84.772465
234.559200-117.441000726534.5592NaN10250 RANCHO ROAD ADELANTO CA 92301ADLNTCAADELANTO ICE PROCESSING CENTER10250 RANCHO ROADADELANTO...206164726GEO3721.02625414191834.557721-117.442524
332.817700-111.520000709632.8177NaN1705 EAST HANNA RD. ELOY AZ 85131EAZELOY FEDERAL CONTRACT FACILITY1705 EAST HANNA RD.ELOY...154232785CCA3721.02502952148932.821231-111.549772
447.249100-122.421000675747.2491NaN1623 E. J STREET TACOMA WA 98421CSCNWWANORTHWEST DETENTION CENTER1623 E. J STREETTACOMA...166174693GEO3721.02519386156347.250214-122.422746
..................................................................
48139.671492-75.71432910.0000NaN970 BROAD STREET NEWARK NJ 7102NEWHOLDNEW/INS OS HOLD ROOM970 BROAD STREETNEWARK...001FEDERAL370.1050239.671492-75.714329
48226.204563-98.27014510.0000NaNBENTSEN TOWER, 1701 W BUS HWY 83 MCALLEN TX 78501USMS3TXUS MARSHALS (SOUTH DISTRICT, TEXAS)BENTSEN TOWER, 1701 W BUS HWY 83MCALLEN...000FEDERAL00.000026.204563-98.270145
48341.528728-73.36354510.0000NaNBRIDGEWATER STATE HOSPITAL BRIDGEWATER MA 2324MABSHOSBRIDGEWATER STATE HOSPITALBRIDGEWATER STATE HOSPITALBRIDGEWATER...000HOSPITAL00.000041.528728-73.363545
484NaNNaN10.0000NaNRedacted Redacted Redacted RedactedRedactedRedactedRedactedRedacted...000ORR170.05171NaNNaN
485NaNNaN10.0000NaNRedacted Redacted Redacted RedactedRedactedRedactedRedactedRedacted...000NaN00.0000NaNNaN
\n", "

486 rows × 51 columns

\n", "
" ], "text/plain": [ " lat lon adpSum onWeb Flags \\\n", "0 28.895000 -99.121200 8391 28.8950 NaN \n", "1 32.036600 -84.771800 8004 32.0366 NaN \n", "2 34.559200 -117.441000 7265 34.5592 NaN \n", "3 32.817700 -111.520000 7096 32.8177 NaN \n", "4 47.249100 -122.421000 6757 47.2491 NaN \n", ".. ... ... ... ... ... \n", "481 39.671492 -75.714329 1 0.0000 NaN \n", "482 26.204563 -98.270145 1 0.0000 NaN \n", "483 41.528728 -73.363545 1 0.0000 NaN \n", "484 NaN NaN 1 0.0000 NaN \n", "485 NaN NaN 1 0.0000 NaN \n", "\n", " fulladdr DETLOC \\\n", "0 566 VETERANS DRIVE PEARSALL TX 78061 STCDFTX \n", "1 146 CCA ROAD LUMPKIN GA 31815 STWRTGA \n", "2 10250 RANCHO ROAD ADELANTO CA 92301 ADLNTCA \n", "3 1705 EAST HANNA RD. ELOY AZ 85131 EAZ \n", "4 1623 E. J STREET TACOMA WA 98421 CSCNWWA \n", ".. ... ... \n", "481 970 BROAD STREET NEWARK NJ 7102 NEWHOLD \n", "482 BENTSEN TOWER, 1701 W BUS HWY 83 MCALLEN TX 78501 USMS3TX \n", "483 BRIDGEWATER STATE HOSPITAL BRIDGEWATER MA 2324 MABSHOS \n", "484 Redacted Redacted Redacted Redacted Redacted \n", "485 Redacted Redacted Redacted Redacted Redacted \n", "\n", " Name Address \\\n", "0 SOUTH TEXAS DETENTION COMPLEX 566 VETERANS DRIVE \n", "1 STEWART DETENTION CENTER 146 CCA ROAD \n", "2 ADELANTO ICE PROCESSING CENTER 10250 RANCHO ROAD \n", "3 ELOY FEDERAL CONTRACT FACILITY 1705 EAST HANNA RD. \n", "4 NORTHWEST DETENTION CENTER 1623 E. J STREET \n", ".. ... ... \n", "481 NEW/INS OS HOLD ROOM 970 BROAD STREET \n", "482 US MARSHALS (SOUTH DISTRICT, TEXAS) BENTSEN TOWER, 1701 W BUS HWY 83 \n", "483 BRIDGEWATER STATE HOSPITAL BRIDGEWATER STATE HOSPITAL \n", "484 Redacted Redacted \n", "485 Redacted Redacted \n", "\n", " City ... ICE.Threat.Level.2 ICE.Threat.Level.3 \\\n", "0 PEARSALL ... 112 311 \n", "1 LUMPKIN ... 344 365 \n", "2 ADELANTO ... 206 164 \n", "3 ELOY ... 154 232 \n", "4 TACOMA ... 166 174 \n", ".. ... ... ... ... \n", "481 NEWARK ... 0 0 \n", "482 MCALLEN ... 0 0 \n", "483 BRIDGEWATER ... 0 0 \n", "484 Redacted ... 0 0 \n", "485 Redacted ... 0 0 \n", "\n", " No.ICE.Threat.Level Facility.Operator FY17.Calendar.Days.in.Use \\\n", "0 1187 GEO 372 \n", "1 671 CCA 372 \n", "2 726 GEO 372 \n", "3 785 CCA 372 \n", "4 693 GEO 372 \n", ".. ... ... ... \n", "481 1 FEDERAL 37 \n", "482 0 FEDERAL 0 \n", "483 0 HOSPITAL 0 \n", "484 0 ORR 17 \n", "485 0 NaN 0 \n", "\n", " FY17...of.Days.in.Use FY17.Total.Mandays FY17.Max.Pop.Count geocodelat \\\n", "0 1.02 598554 1854 28.896498 \n", "1 1.02 671515 1992 32.037982 \n", "2 1.02 625414 1918 34.557721 \n", "3 1.02 502952 1489 32.821231 \n", "4 1.02 519386 1563 47.250214 \n", ".. ... ... ... ... \n", "481 0.10 50 2 39.671492 \n", "482 0.00 0 0 26.204563 \n", "483 0.00 0 0 41.528728 \n", "484 0.05 17 1 NaN \n", "485 0.00 0 0 NaN \n", "\n", " geocodelon \n", "0 -99.116863 \n", "1 -84.772465 \n", "2 -117.442524 \n", "3 -111.549772 \n", "4 -122.422746 \n", ".. ... \n", "481 -75.714329 \n", "482 -98.270145 \n", "483 -73.363545 \n", "484 NaN \n", "485 NaN \n", "\n", "[486 rows x 51 columns]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ICE_df = pd.read_csv(\"../data/ICE-facilities.csv\")\n", "ICE_df" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "US_map = folium.Map(location=[42, -102], zoom_start=4)\n", "US_map" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "ICE_df = ICE_df.dropna(subset=['lat', 'lon'])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ICE_df.apply(create_ICE_map_markers, map_name=US_map, axis=\"columns\")\n", "US_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choropleth Maps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```margin Choropleth Map\n", "Choropleth map = a map where areas are shaded according to a value\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data in this section was drawn from [Torn Apart / Separados Project](https://github.com/xpmethod/torn-apart-open-data). This data maps the \"cumulative ICE awards since 2014 to contractors by congressional district,\" as featured in [Volume 2](http://xpmethod.plaintext.in/torn-apart/volume/2/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a chropleth map with Folium, we need to pair a \"geo.json\" file (which indicates which parts of the map to shade) with a CSV file (which includes the variable that we want to shade by)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following data was drawn from [the Torn Apart / Separados project](https://github.com/xpmethod/torn-apart/tree/master/data/districts)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "US_districts_geo_json = \"../data/ICE_money_districts.geo.json\"" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "US_districts_csv = pd.read_csv(\"../data/ICE_money_districts.csv\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "US_districts_csv = US_districts_csv .dropna(subset=['districtName', 'representative'])" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idid2statedistrictNumberdistrictNamepartydistrict_urlrepresentativerepresentative_photo_urltotal_awards
05001500US0101101Alabama1ta-ordinal-st-mrepublicanhttps://en.wikipedia.org/wiki/Alabama%27s_1st_congressional_districtBradley Byrnehttps://upload.wikimedia.org/wikipedia/commons/7/71/Rep_Bradley_Byrne_%28cropped%29.jpg0.00
15001500US0102102Alabama2ta-ordinal-nd-mrepublicanhttps://en.wikipedia.org/wiki/Alabama%27s_2nd_congressional_districtMartha Robyhttps://upload.wikimedia.org/wikipedia/commons/5/55/Martha_roby_113_congressional_portrait_%28cropped%29.jpg38577.40
25001500US0103103Alabama3ta-ordinal-rd-mrepublicanhttps://en.wikipedia.org/wiki/Alabama%27s_3rd_congressional_districtMike Rogershttps://upload.wikimedia.org/wikipedia/commons/e/ee/Mike_Rogers_official_photo_%28cropped%29.jpg0.00
35001500US0104104Alabama4ta-ordinal-th-mrepublicanhttps://en.wikipedia.org/wiki/Alabama%27s_4th_congressional_districtRobert Aderholthttps://upload.wikimedia.org/wikipedia/commons/9/9f/Rep._Robert_B._Aderholt_%28cropped%29.jpg171873.55
45001500US0105105Alabama5ta-ordinal-th-mrepublicanhttps://en.wikipedia.org/wiki/Alabama%27s_5th_congressional_districtMo Brookshttps://upload.wikimedia.org/wikipedia/commons/b/b6/Mo_Brooks_Portrait_%28cropped%29.jpg40346.00
.................................
4325001500US55065506Wisconsin6ta-ordinal-th-mrepublicanhttps://en.wikipedia.org/wiki/Wisconsin%27s_6th_congressional_districtGlenn Grothmanhttps://upload.wikimedia.org/wikipedia/commons/1/16/Glenn_Grothman_official_congressional_photo_%28cropped%29.jpg3242401.61
4335001500US55075507Wisconsin7ta-ordinal-th-mrepublicanhttps://en.wikipedia.org/wiki/Wisconsin%27s_7th_congressional_districtSean Duffyhttps://upload.wikimedia.org/wikipedia/commons/d/d7/Sean_Duffy_official_congressional_photo_%28cropped%29.jpg32698.55
4345001500US55085508Wisconsin8ta-ordinal-th-mrepublicanhttps://en.wikipedia.org/wiki/Wisconsin%27s_8th_congressional_districtMike Gallagherhttps://upload.wikimedia.org/wikipedia/commons/a/ad/Mike_Gallagher_Official_Portrait_2017_%28cropped%29.png237392.73
4355001500US56005600Wyoming0ta-at-large-districtrepublicanhttps://en.wikipedia.org/wiki/Wyoming%27s_at-large_congressional_districtLiz Cheneyhttps://upload.wikimedia.org/wikipedia/commons/d/dd/Liz_Cheney_official_portrait.jpg0.00
4365001500US72987298Puerto Rico98ta-ordinal-th-mno-rephttps://en.wikipedia.org/wiki/Puerto_Rico%27s_at-large_congressional_districtResident Commissioner Jenniffer Gonzálezhttps://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg/800px-Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg323102.10
\n", "

431 rows × 10 columns

\n", "
" ], "text/plain": [ " id id2 state districtNumber districtName \\\n", "0 5001500US0101 101 Alabama 1 ta-ordinal-st-m \n", "1 5001500US0102 102 Alabama 2 ta-ordinal-nd-m \n", "2 5001500US0103 103 Alabama 3 ta-ordinal-rd-m \n", "3 5001500US0104 104 Alabama 4 ta-ordinal-th-m \n", "4 5001500US0105 105 Alabama 5 ta-ordinal-th-m \n", ".. ... ... ... ... ... \n", "432 5001500US5506 5506 Wisconsin 6 ta-ordinal-th-m \n", "433 5001500US5507 5507 Wisconsin 7 ta-ordinal-th-m \n", "434 5001500US5508 5508 Wisconsin 8 ta-ordinal-th-m \n", "435 5001500US5600 5600 Wyoming 0 ta-at-large-district \n", "436 5001500US7298 7298 Puerto Rico 98 ta-ordinal-th-m \n", "\n", " party \\\n", "0 republican \n", "1 republican \n", "2 republican \n", "3 republican \n", "4 republican \n", ".. ... \n", "432 republican \n", "433 republican \n", "434 republican \n", "435 republican \n", "436 no-rep \n", "\n", " district_url \\\n", "0 https://en.wikipedia.org/wiki/Alabama%27s_1st_congressional_district \n", "1 https://en.wikipedia.org/wiki/Alabama%27s_2nd_congressional_district \n", "2 https://en.wikipedia.org/wiki/Alabama%27s_3rd_congressional_district \n", "3 https://en.wikipedia.org/wiki/Alabama%27s_4th_congressional_district \n", "4 https://en.wikipedia.org/wiki/Alabama%27s_5th_congressional_district \n", ".. ... \n", "432 https://en.wikipedia.org/wiki/Wisconsin%27s_6th_congressional_district \n", "433 https://en.wikipedia.org/wiki/Wisconsin%27s_7th_congressional_district \n", "434 https://en.wikipedia.org/wiki/Wisconsin%27s_8th_congressional_district \n", "435 https://en.wikipedia.org/wiki/Wyoming%27s_at-large_congressional_district \n", "436 https://en.wikipedia.org/wiki/Puerto_Rico%27s_at-large_congressional_district \n", "\n", " representative \\\n", "0 Bradley Byrne \n", "1 Martha Roby \n", "2 Mike Rogers \n", "3 Robert Aderholt \n", "4 Mo Brooks \n", ".. ... \n", "432 Glenn Grothman \n", "433 Sean Duffy \n", "434 Mike Gallagher \n", "435 Liz Cheney \n", "436 Resident Commissioner Jenniffer González \n", "\n", " representative_photo_url \\\n", "0 https://upload.wikimedia.org/wikipedia/commons/7/71/Rep_Bradley_Byrne_%28cropped%29.jpg \n", "1 https://upload.wikimedia.org/wikipedia/commons/5/55/Martha_roby_113_congressional_portrait_%28cropped%29.jpg \n", "2 https://upload.wikimedia.org/wikipedia/commons/e/ee/Mike_Rogers_official_photo_%28cropped%29.jpg \n", "3 https://upload.wikimedia.org/wikipedia/commons/9/9f/Rep._Robert_B._Aderholt_%28cropped%29.jpg \n", "4 https://upload.wikimedia.org/wikipedia/commons/b/b6/Mo_Brooks_Portrait_%28cropped%29.jpg \n", ".. ... \n", "432 https://upload.wikimedia.org/wikipedia/commons/1/16/Glenn_Grothman_official_congressional_photo_%28cropped%29.jpg \n", "433 https://upload.wikimedia.org/wikipedia/commons/d/d7/Sean_Duffy_official_congressional_photo_%28cropped%29.jpg \n", "434 https://upload.wikimedia.org/wikipedia/commons/a/ad/Mike_Gallagher_Official_Portrait_2017_%28cropped%29.png \n", "435 https://upload.wikimedia.org/wikipedia/commons/d/dd/Liz_Cheney_official_portrait.jpg \n", "436 https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg/800px-Official_portrait_of_Resident_Commissioner_Jenniffer_Gonzalez.jpg \n", "\n", " total_awards \n", "0 0.00 \n", "1 38577.40 \n", "2 0.00 \n", "3 171873.55 \n", "4 40346.00 \n", ".. ... \n", "432 3242401.61 \n", "433 32698.55 \n", "434 237392.73 \n", "435 0.00 \n", "436 323102.10 \n", "\n", "[431 rows x 10 columns]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "US_districts_csv" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "US_map = folium.Map(location=[42, -102], zoom_start=4)\n", "\n", "folium.Choropleth(\n", " geo_data = US_districts_geo_json,\n", " name = 'choropleth',\n", " data = US_districts_csv,\n", " columns = ['districtName', 'total_awards'],\n", " key_on = 'feature.properties.districtName',\n", " fill_color = 'GnBu',\n", " line_opacity = 0.2,\n", " legend_name= 'Total ICE Money Received'\n", ").add_to(US_map)\n", "\n", "US_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a Tooltip to Choropleth" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tooltip = folium.features.GeoJson(\n", " US_districts_geo_json,\n", " tooltip=folium.features.GeoJsonTooltip(fields=['representative', 'state', 'party', 'total_value'], localize=True)\n", " )\n", "US_map.add_child(tooltip)\n", "US_map" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }