Skip to content

Instantly share code, notes, and snippets.

View justinm0rgan's full-sized avatar

Justin Morgan Williams justinm0rgan

View GitHub Profile
@justinm0rgan
justinm0rgan / bar_chart_dash.py
Last active August 5, 2023 14:25
Bar chart and callback function for within dash app
import pandas as pd
import plotly.graph_objects as go
# create race graph
def create_race_graph(df_filtered):
# Select columns for bar graph
race_columns = ["WtNHP", "BlNHP", "AsnNHP", "Hsp1P", "OthNHP", "Rc2plNHP"]
race_labels = [
"White",
"Black or African American",
@justinm0rgan
justinm0rgan / bar_chart.py
Last active August 4, 2023 13:37
Bar chart filtered for one CDTA
import pandas as pd
import plotly.graph_objects as go
# filter data for BK01
df_bk01 = df_acs_cdta_2021[df_acs_cdta_2021['GeoID'].str.contains('BK01')]
# select columns for bar graph
race_columns = ['WtNHP', 'BlNHP', 'AsnNHP', 'Hsp1P', 'OthNHP', 'Rc2plNHP']
race_labels = ['White', 'Black or African American', 'Asian', 'Hispanic or Latino',
'Some Other Race', 'Two or More Races']
@justinm0rgan
justinm0rgan / parks_cdta_plotly.py
Last active August 3, 2023 23:01
Map with cdta and parks in plotly with toggle buttons
import geopandas as gpd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio
# convert to 4326 for plotly
gdf_parks.to_crs(4326, inplace=True)
gdf_cdta.to_crs(4326, inplace=True)
# define trace for parks layer
@justinm0rgan
justinm0rgan / parks_cdta_plot.py
Created August 3, 2023 22:50
Geopandas plot of cdta and parks on same map
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
# convert to better crs for gpd
gdf_parks.to_crs(2263, inplace=True)
gdf_cdta.to_crs(2263, inplace=True)
# new plot
@justinm0rgan
justinm0rgan / cdta_map_plotly.py
Last active August 3, 2023 23:05
Maps CDTA in plotly with acres as z
import geopandas as gpd
import plotly.graph_objects as go
import plotly.io as pio
import config
# convert to 4326 to view in plotly
gdf_cdta.to_crs(4326,inplace=True)
# define a trace
@justinm0rgan
justinm0rgan / cdta.py
Created August 3, 2023 22:25
Download community district tabulation areas
import geopandas as gpd
# import cdta
cdta_url="https://data.cityofnewyork.us/api/geospatial/xn3r-zk6y?accessType=DOWNLOAD&method=export&format=Shapefile"
gdf_cdta=gpd.read_file(cdta_url)
# add acre column in gdf_cdta
gdf_cdta['cdta_acres'] = round(gdf_cdta['shape_area'] / 43560)
# get info
@justinm0rgan
justinm0rgan / plot_parks.py
Last active August 3, 2023 22:04
Plot the parks properties
import geopandas as gpd
import matplotlib.pyplot as plt
import contextily as ctx
# create a new plot using geopandas
fig, ax = plt.subplots(figsize=(10,10))
gdf_parks.plot(ax=ax,edgecolor='black', alpha=0.5)
# add any additional features to the plot
ax.set_title('NYC Park Properties', fontdict={"size":20})
@justinm0rgan
justinm0rgan / parks_properties.py
Created August 3, 2023 21:56
Download parks properties
import geopandas as gpd
# define url
parks_url = 'https://data.cityofnewyork.us/api/geospatial/enfh-gkve?method=export&format=Shapefile'
gdf_parks = gpd.read_file(parks_url) # import shapefile as gdf
# change crs
gdf_parks.to_crs(2263, inplace=True)
# preview gdf
# import packages
import matplotlib.pyplot as plt
import contextily as ctx
# plot map use helper function
def plot_column_from_dict_facecolor(gdf, col_label_cmap_dict, jstevens_col_mats,
alpha=0.85, font_size=14, save=False):
"""
Takes in gdf, column label cmap dictionary, and color matrix
Also uses plot_inset_legend helper function to create plots
@justinm0rgan
justinm0rgan / plot_inset_legend.py
Created May 31, 2023 02:42
Helper function to plot inset legend for bivariate choropleth
# import necessary packages
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.colors as mcolors
import matplotlib.collections as mcollect
# helper function to plot inset legend
def plot_inset_legend(ax, color_matrix, x_label='X Label'):
"""
Takes in axes from plot, color_matrix and optional x label arg