In [1]:
import pandas as pd
pd.set_option('display.max_columns',None)

import matplotlib.pyplot as plt
import plotly.graph_objects as go
import plotly.express as px
In [2]:
Count_by_year=pd.read_csv(r'Count_by_year.csv')
Count_by_year.head()
Out[2]:
Crime_Type Year Count
0 THEFT 2016 61617
1 BATTERY 2016 50298
2 CRIMINAL DAMAGE 2016 31018
3 DECEPTIVE PRACTICE 2016 19272
4 ASSAULT 2016 18741
In [40]:
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()
In [51]:
colorscale='Bluered'

fig=px.bar(Count_by_year,x='Crime_Type',y='Count',animation_frame='Year',
       category_orders={'Crime_Type':Count_by_year.Crime_Type.unique()},
      color_continuous_scale = [(0, "rgb(0, 119, 182)"), (0.1, "rgb(92, 77, 125)"), (1, "rgb(255, 0, 43)")],
           color='Count',log_y=True,template='seaborn')
fig.update_layout(
    title={
        'text': "Yearly Crimes Count by Type",
        'font': dict(size=20),
        'y':0.975,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'},
    template='seaborn',
    yaxis=dict(type="log",title='Crime Count'),
    xaxis = dict(title='Select Year with Slider',tickangle=-45),
    plot_bgcolor = '#f5ebe0',
    paper_bgcolor="#edede9",
    font_family="Roboto Slab"
)
fig["layout"].pop("updatemenus")
# fig.update_traces(
#     marker=dict(line=dict(color=bar_bordercolor,width=2))
# )
fig.update(layout_coloraxis_showscale=False)
fig.update_yaxes(range=[0, 5])

fig['layout']['sliders'][0]['pad']=dict(r= 10, t= 200,l=-70)
fig.write_html('CrimeCountBarplot2.html',config={'responsive':False})
fig.show()
In [ ]: