import pandas as pd
import numpy as np
import plotly.graph_objects as go
data = pd.read_csv("crime_clean.csv")
df_radar = data[["hour","date","primary_type"]]\
.groupby(["hour","primary_type"]).count().reset_index().rename(columns={"date":"count"})
df_radar.hour = pd.DataFrame(["0"+str(i) if len(str(i))==1 else str(i) for i in df_radar.hour])
df_radar
hour | primary_type | count | |
---|---|---|---|
0 | 00 | ARSON | 205 |
1 | 00 | ASSAULT | 3859 |
2 | 00 | BATTERY | 14428 |
3 | 00 | BURGLARY | 3065 |
4 | 00 | CONCEALED CARRY LICENSE VIOLATION | 42 |
... | ... | ... | ... |
693 | 23 | ROBBERY | 3227 |
694 | 23 | SEX OFFENSE | 716 |
695 | 23 | STALKING | 48 |
696 | 23 | THEFT | 10420 |
697 | 23 | WEAPONS VIOLATION | 2941 |
698 rows × 3 columns
layout=go.Layout(polar=dict(angularaxis = dict(rotation = 90,direction = "clockwise",tickfont=dict(size=15))))
fig = go.Figure(data=[
go.Scatterpolar(
name = "THEFT",
r=df_radar[df_radar.primary_type=="THEFT"]["count"].append(df_radar[(df_radar.primary_type=="THEFT")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="THEFT"]["hour"].append(df_radar[(df_radar.primary_type=="THEFT")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#001219", shape="spline", smoothing=1)),
go.Scatterpolar(
name = "BATTERY",
r=df_radar[df_radar.primary_type=="BATTERY"]["count"].append(df_radar[(df_radar.primary_type=="BATTERY")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="BATTERY"]["hour"].append(df_radar[(df_radar.primary_type=="BATTERY")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#005f73", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "CRIMINAL DAMAGE",
r=df_radar[df_radar.primary_type=="CRIMINAL DAMAGE"]["count"].append(df_radar[(df_radar.primary_type=="CRIMINAL DAMAGE")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="CRIMINAL DAMAGE"]["hour"].append(df_radar[(df_radar.primary_type=="CRIMINAL DAMAGE")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#0a9396", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "ASSAULT",
r=df_radar[df_radar.primary_type=="ASSAULT"]["count"].append(df_radar[(df_radar.primary_type=="ASSAULT")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="ASSAULT"]["hour"].append(df_radar[(df_radar.primary_type=="ASSAULT")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#94d2bd", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "ROBBERY",
r=df_radar[df_radar.primary_type=="ROBBERY"]["count"].append(df_radar[(df_radar.primary_type=="ROBBERY")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="ROBBERY"]["hour"].append(df_radar[(df_radar.primary_type=="ROBBERY")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#e9d8a6", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "DECEPTIVE PRACTICE",
r=df_radar[df_radar.primary_type=="DECEPTIVE PRACTICE"]["count"].append(df_radar[(df_radar.primary_type=="DECEPTIVE PRACTICE")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="DECEPTIVE PRACTICE"]["hour"].append(df_radar[(df_radar.primary_type=="DECEPTIVE PRACTICE")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#ee9b00", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "NARCOTICS",
r=df_radar[df_radar.primary_type=="NARCOTICS"]["count"].append(df_radar[(df_radar.primary_type=="NARCOTICS")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="NARCOTICS"]["hour"].append(df_radar[(df_radar.primary_type=="NARCOTICS")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#ca6702", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "MOTOR VEHICLE THEFT",
r=df_radar[df_radar.primary_type=="MOTOR VEHICLE THEFT"]["count"].append(df_radar[(df_radar.primary_type=="MOTOR VEHICLE THEFT")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="MOTOR VEHICLE THEFT"]["hour"].append(df_radar[(df_radar.primary_type=="MOTOR VEHICLE THEFT")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#bb3e03", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "BURGLARY",
r=df_radar[df_radar.primary_type=="BURGLARY"]["count"].append(df_radar[(df_radar.primary_type=="BURGLARY")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="BURGLARY"]["hour"].append(df_radar[(df_radar.primary_type=="BURGLARY")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#ae2012", shape="spline", smoothing=0.7)),
go.Scatterpolar(
name = "WEAPONS VIOLATION",
r=df_radar[df_radar.primary_type=="WEAPONS VIOLATION"]["count"].append(df_radar[(df_radar.primary_type=="WEAPONS VIOLATION")&(df_radar.hour=="00")]["count"]),
theta=df_radar[df_radar.primary_type=="WEAPONS VIOLATION"]["hour"].append(df_radar[(df_radar.primary_type=="WEAPONS VIOLATION")&(df_radar.hour=="00")]["hour"]),
line=dict(color="#9b2226", shape="spline", smoothing=0.7))],
layout=layout)
fig.update_layout(
updatemenus=[
dict(font=dict(size=13),
active=0,
buttons=list([
dict(label="ALL",
method="update",
args=[{"visible": [True, True, True, True, True, True, True, True, True, True]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>Top 10 Crime Hourly Distribution</span>"}]),
dict(label="THEFT",
method="update",
args=[{"visible": [True, False, False, False, False, False, False, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>THEFT Hourly Distribution</span>",
}]),
dict(label="BATTERY",
method="update",
args=[{"visible": [False, True, False, False, False, False, False, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>BATTERY Hourly Distribution</span>",
}]),
dict(label="CRIMINAL DAMAGE",
method="update",
args=[{"visible": [False, False, True, False, False, False, False, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>CRIMINAL DAMAGE Hourly Distribution</span>",
}]),
dict(label="ASSAULT",
method="update",
args=[{"visible": [False, False, False, True, False, False, False, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>ASSAULT Hourly Distribution</span>",
}]),
dict(label="ROBBERY",
method="update",
args=[{"visible": [False, False, False, False, True, False, False, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>ROBBERY Hourly Distribution</span>",
}]),
dict(label="DECEPTIVE PRACTICE",
method="update",
args=[{"visible": [False, False, False, False, False, True, False, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>DECEPTIVE PRACTICE Hourly Distribution</span>",
}]),
dict(label="NARCOTICS",
method="update",
args=[{"visible": [False, False, False, False, False, False, True, False, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>NARCOTICS Hourly Distribution</span>",
}]),
dict(label="MOTOR VEHICLE THEFT",
method="update",
args=[{"visible": [False, False, False, False, False, False, False, True, False, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>MOTOR VEHICLE THEFT Hourly Distribution</span>",
}]),
dict(label="BURGLARY",
method="update",
args=[{"visible": [False, False, False, False, False, False, False, False, True, False]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>BURGLARY Hourly Distribution</span>",
}]),
dict(label="WEAPONS VIOLATION",
method="update",
args=[{"visible": [False, False, False, False, False, False, False, False, False, True]},
{"title": "<span style='font-size: 26px; font-weight:bold; '>WEAPONS VIOLATION Hourly Distribution</span>",
}]),
]),
)
])
fig.update_layout(
paper_bgcolor="#edede9",
plot_bgcolor="#f5ebe0",
font_family="Roboto Slab",
title_font_family="Roboto Slab",
polar=dict(radialaxis=dict(visible=True)),
showlegend=True,
template="seaborn",
width=850, height=600,
title_text = "<span style='font-size: 20px; font-weight:bold; '>Chicago Crime Hourly Distribution</span>",
title_x=0.5,
legend=dict(font=dict(size=13), yanchor="top",y=0.9,xanchor="left", x=-0.4),
title = dict(font=dict(size=20))
)
fig.show()
fig.write_html("radar_plot.html")