Created
April 14, 2020 22:10
-
-
Save thomashikaru/0ba4e0dede890371c9cf7647acd68f15 to your computer and use it in GitHub Desktop.
Visualizing density in New York City's 5 boroughs by neighborhood
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import json | |
import math | |
import plotly.express as px | |
from area import area | |
# read the neighborhood population data into a DataFrame and load the GeoJSON data | |
df = pd.read_csv('New_York_City_Population_By_Neighborhood_Tabulation_Areas.csv') | |
nycmap = json.load(open("nyc_neighborhoods.geojson")) | |
# create dictionary of nta codes mapping to area (square miles) | |
d = {} | |
neighborhood = nycmap["features"] | |
for n in neighborhood: | |
code = n["properties"]["ntacode"] | |
a = area(n["geometry"])/(1609*1609) # converts from m^2 to mi^2 | |
d[code] = a | |
# create new columns in df for area and density | |
df["area"] = df["NTA Code"].map(d) | |
df = df.dropna(subset=["area"]) | |
df["density"] = df["Population"]/df["area"] | |
# call Plotly Express choropleth function to visualize data | |
fig = px.choropleth_mapbox(df, | |
geojson=nycmap, | |
locations="NTA Code", | |
featureidkey="properties.ntacode", | |
color="density", | |
color_continuous_scale="viridis", | |
mapbox_style="carto-positron", | |
zoom=9, center={"lat": 40.7, "lon": -73.9}, | |
opacity=0.7, | |
hover_name="NTA Name" | |
) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment