简体   繁体   中英

Choropleth map - no color differentiation

I'm trying to create a choropleth map based on rates of unaffordable housing for Toronto neighborhood. I found a dataset online which I read in aa csv, then conditioned data to make appropriate columns, and then attempted to create my map.

My code for the data and conditioning looks like this:

df = pd.read_csv('torontodata.csv')
df = df.transpose()
df.columns = df.iloc[4]
df.drop(['_id' , 'Category', 'Topic', 'Data Source', 'Characteristic'], axis=0, inplace=True)
df = df.reset_index()
df = pd.DataFrame(df[['index', 'Rate of unaffordable housing']])
df.drop(df.loc[df['index']=='City of Toronto'].index, inplace=True)
df['Rate of unaffordable housing'] = df['Rate of unaffordable housing'].astype(float)

I'm fairly confident that this was done correctly because it returns exactly as it should: dataframe . I think the error must be in the 'key_on' parameter of the choropleth map, but I don't know what I did wrong. I checked the raw geojson file and it seems like I have the correct path.

!wget --quiet https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson

print('GeoJSON file downloaded!')
toronto_geo = r'toronto_crs84.geojson' # geojson file

address = 'Toronto, ON'

geolocator = Nominatim(user_agent="foursquare_agent")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print(latitude, longitude)

toronto_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map centred around Toronto
threshold_scale = np.linspace(df['Rate of unaffordable housing'].min(),
                              df['Rate of unaffordable housing'].max(),
                              6, dtype=int)
threshold_scale = threshold_scale.tolist() # change the numpy array to a list
threshold_scale[-1] = threshold_scale[-1] + 1 # make sure that the last value of the list is greater than the maximum immigration


# display map

toronto_map.choropleth(
    geo_data=toronto_geo,
    data=df,
    columns=['index', 'Rate of unaffordable housing'],    
    key_on='feature.properties.name', 
    threshold_scale=threshold_scale, 
    fill_color='YlOrRd', 
    fill_opacity=0.3, 
    line_opacity=0.2,
    legend_name='Affordable housing in Canada'
)
toronto_map

This doesn't return an error, but my map is all one color value. I've been stuck on this problem for an entire day. I even tried using a different json file but I had the same problem. Any help would be appreciated.

https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/toronto.geojson

The data you are trying to read is a JSON file, not a CSV file.

import json

with open('toronto.geojson.txt', 'rb') as f:
    data = json.load(f)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM