In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import folium
import geopandas as gpd
import plotly.express as px
import json
In [2]:
community_crime = pd.read_csv('crime_by_community.csv')
In [3]:
fh = open('chicago_geo.geojson')
commu_geo_json = json.load(fh)
fh.close
Out[3]:
<function TextIOWrapper.close()>
In [4]:
for feature in commu_geo_json['features']:
    feature.update({'id': feature['properties']['area_num_1']})
In [5]:
community_crime
Out[5]:
Year Community Area Crime Count Community
0 2016 1 3637 ROGERS PARK
1 2016 2 3256 WEST RIDGE
2 2016 3 3515 UPTOWN
3 2016 4 1957 LINCOLN SQUARE
4 2016 5 1369 NORTH CENTER
... ... ... ... ...
457 2021 73 2498 WASHINGTON HEIGHTS
458 2021 74 490 MOUNT GREENWOOD
459 2021 75 1713 MORGAN PARK
460 2021 76 1394 OHARE
461 2021 77 2365 EDGEWATER

462 rows × 4 columns

In [9]:
fig = px.choropleth_mapbox(community_crime, geojson = commu_geo_json, locations = 'Community Area',
                           color = 'Crime Count', animation_frame = 'Year',
                           color_continuous_scale = [(0, "rgb(0, 119, 182)"), (0.5, "rgb(92, 77, 125)"), (1, "rgb(255, 0, 43)")],
                           mapbox_style = "carto-positron", range_color = [1000, 8000],
                           zoom = 9, center = {"lat": 41.85, "lon": -87.7},
                           opacity = 0.6, 
                           hover_data = {'Community Area': False,
                                         'Community': True,
                                        'Crime Count': True},
                           width  = 980, height = 600, title = '<span style="font-weight: bold;">Chicago Yearly Crime Count Community Map</span>'
                          )
fig.update_layout(margin={"r":0,"t":80,"l":10,"b":10})
fig.update_layout(
    font_family="Roboto Slab",
    title_font_family="Roboto Slab",
    title_font_size = 20,
    paper_bgcolor="#edede9",
    plot_bgcolor="#f5ebe0"
)
fig.update_layout(
    hoverlabel = dict(
        bgcolor = "black",
        font_size = 10,
        font_family = "Roboto Slab"
    )
)
fig["layout"].pop("updatemenus")
fig['layout']['sliders'][0]['pad']=dict(t= 10, b = 30)
fig.show()
In [7]:
fig.write_html("crime_map_community.html")
In [ ]:
 
In [ ]: