Crimetypes=Count_by_year.Crime_Type.unique()
Crimetypes.sort()
# bar_bordercolor='MediumPurple'
bar_color='rgb(92, 77, 125)'
line_color='#ef476f'
fig=go.Figure(layout_title_text="Different Crime Types Cases Count by Year")
fig.add_trace(go.Bar(x=list(range(2016,2022)),y=Count_by_year[Count_by_year.Crime_Type==Crimetypes[0]].Count,
visible=True,name=Crimetypes[0],width=0.5, marker_color=bar_color))
# fig.add_trace(go.Scatter(x=list(range(2016,2022)),y=Count_by_year[Count_by_year.Crime_Type==Crimetypes[0]].Count,
# visible=True,name=Crimetypes[0],mode='lines+markers',line = dict(color=line_color,width=3)))
for crime in Crimetypes[1:]:
fig.add_trace(go.Bar(x=list(range(2016,2022)),y=Count_by_year[Count_by_year.Crime_Type==crime].Count,
visible=False,name=crime,width=0.5, marker_color=bar_color))
# fig.add_trace(go.Scatter(x=list(range(2016,2022)),y=Count_by_year[Count_by_year.Crime_Type==crime].Count,
# visible=False,name=crime,mode='lines+markers',line = dict(color=line_color,width=3)))
buttons=[dict(label=Crimetypes[i],
method="update",
args=[{"visible":[False]*2*i+[True]*2+[False]*(34-i)*2}])
for i in range(32)]
fig.update_layout(
updatemenus=[
dict(
x=1.5,y=0.9,
buttons=buttons
)
],
annotations=[dict(text="Current Crime Type", showarrow=False,
font=dict(size=15),x=1.27, y=1,
yref="paper", align="left",xref='paper', textangle=0)],
template='seaborn',
yaxis=dict(title='Case Count'),
xaxis = dict(title='Year',tickmode = 'array',tickvals = list(range(2016,2022)),ticktext=list(range(2016,2022))),
plot_bgcolor = '#f5ebe0',
paper_bgcolor="#edede9",
margin=dict(l=16, r=16, t=56, b=16),
showlegend=False,
font_family="Roboto Slab",
title_font_size = 20
)
fig.update_traces(
# marker=dict(line=dict(color=bar_bordercolor,width=2)),
hovertemplate=r'Year: %{x}<br>Case Count: %{y}<br>'
)
fig.write_html('CrimeCountBarplot3.html')
fig.show()