0

I'm working with Folium for the first time, and attempting to make a Choropleth map of housing values in North Carolina using Zillow data as the source. I've been running into lots of issues along the way, and right now I'm a bit stuck on how to add in colors to the map; if the property value is >100k make it green, and slowing increasing the gradient to orange if it's <850k.

At the moment the map does generate the zip code data fine, but all of the polygons are a black-grey color. It's also not showing a color key or map name, and I have a feeling some of my earlier code could be off.

import folium
import pandas as pd
import requests
import os
working_directory = os.getcwd()
print(working_directory)

path = working_directory + '/Desktop/NCHomes.csv'
df = pd.read_csv(path)
df.head()

df['Homes'].min(), df['Homes'].max()

INDICATOR = 'North Carolina Home Values by Zip Code'

data = df[df['RegionName'] == INDICATOR]
max_value = data['Homes'].max()

data = data[data['Homes'] == max_value]
data.head()

geojson_url = 'https://raw.githubusercontent.com/OpenDataDE/State-zip-code-GeoJSON/master/nc_north_carolina_zip_codes_geo.min.json'
response = requests.get(geojson_url)
geojson = response.json()
geojson

geojson['features'][0]

map_data = data[['RegionName', 'Homes']]
map_data.head()

M = folium.Map(location=[20, 10], zoom_start=2)
folium.Choropleth(
    geo_data=geojson,
    data=map_data,
    columns=['RegionName', 'Homes'],
    fill_color='YlOrRd',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name=INDICATOR
).add_to(M)

M

1 Answers1

0

You can specify the threshold_scale parameter as follows:

folium.Choropleth(
    geo_data=geojson,
    data=map_data,
    columns=['RegionName', 'Homes'],
    fill_color='YlOrRd',
    fill_opacity=0.7,
    line_opacity=0.2,
    threshold_scale=[100000, 850000],
    legend_name=INDICATOR
).add_to(M)
Nova
  • 406
  • 2
  • 13