

Create WHO statistics data dashboard in python by plotly dash using plotly interactive data visualization library and dash web app library.
There are three charts in this WHO statistics data dashboard in python by plotly dash. Two charts are dynamically changed and one chart is static. Choropleth static map chart is not connected to drop down list and it shows data for each country on the chart. This view can be seen by hovering on the chart each country geographic location. Line chart displays data for each country over the specific period. This chart is connected to the drop down list and selecting any country name from the list it shows data for that selected country. Similarly horizontal bar chart displays data for top age group for selected country.
Import Library
Lines 1-6
Start creating this dashboard by importing necessary library. Make sure you have installed this library in your machine or code editor. I recommend you PyCharm editor to run dash apps in this code editor. I have used dash library, dash html components, dash core components, plotly interactive graph objects data visualization library and pandas for data analysis and manipulating data for creating this dashboard.
Data
Line 10
Download the CSV data file using the below link above the code template. Use pandas read function to read this file. Give the name this data frame as you want because this data frame will be used below in the code during the development of dashboard.
Title
Lines 13-28
Let’s start to create this dashboard. Follow the code below in the code template and copy the code and paste in your code editor. First of all create layout of dash app. Inside the layout of dash app give the name of this dashboard and also give author name of this dashboard right side of the title. Style these two names using html style properties and place these two names according to your system screen resolution.
Drop Down List
Lines 32-52
Next I create input component for this dashboard using dash core components library of dash. I have inserted only one input component that is drop down list. Before I have created layout of choropleth map chart. This map chart is static chart and it is not connected to the drop down list. Use name of data frame and name of column in options parameter to get unique country name list in the drop down list.
Charts
Lines 55-73
Create the layout of remaining two charts. Give unique id to these two charts. Adjust the position of these two charts according to your system screen resolution using html style properties. I have not used dash bootstrap components in this dashboard. Dash bootstrap components automatically adjust the layout of app in any screen resolution device.
Data and Layout for each chart
Lines 108-667
First I create data frame for Choropleth map chart. You can view that data frame below in the code template line 79. In data section of this chart, you can see in line 84 I have used locations parameter to display geographic location of each country on the map chart from to the country column of data frame. z parameter is used in Choropleth map chart to display data on each geographic location of country on the map chart. Use color scale to show unique country location by color and create hover effect for each country location.
Line chart is second chart in this dashboard. You can see in the above image, there are six lines. Each line represents data for age group and by selecting any country name from the list, data is changed on the line chart according to the selected country over the period. x-axis of this chart shows year values and y-axis shows no. of suicides for selected country. Mode parameter is used in line chart to display line on the line chart. We can also add markers property in the mode parameter to display markers on the line.
Third chart is Horizontal bar chart. This horizontal bar chart displays values for top age group for the selected country in the drop down list. Create new data frame for this chart. You can view this data frame in the code template lines 305 to 306. You can see in the above image, each horizontal bar displays data. I have created text template for this. Select text position auto because text will automatically adjusted inside the bars or outside the bars. It depends on the length of bars. You can view more details below in the code template lines 312 to 314.
Download the csv data file using the below link.
https://www.kaggle.com/szamil/who-suicide-statistics
To see full code,hover on the code area and use horizontal scrollbar if needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output import plotly.graph_objs as go import pandas as pd suicide = pd.read_csv('who_suicide_statistics.csv') app = dash.Dash(__name__,) app.layout = html.Div([ html.Div([ html.Br(), html.Br(), html.H1('Who Suicide Statistics Data Dashboard')], style={'margin-left': '5%','color':'#808000','width': '50%', 'display': 'inline-block' }), html.Div([ html.Br(), html.Br(), html.H4('Prepared by: Mubeen Ali')], style={'color':'#17202A','width': '30%', 'display': 'inline-block', 'float': 'right' }), # Create choropleth map chart html.Div([ html.Br(), dcc.Graph(id='map_1', config={'displayModeBar': 'hover'}), ], style={'margin-left': '1.65%'}), html.Br(), html.Br(), html.Div([ html.Label('Select a Country:'), dcc.Dropdown(id='w_countries', multi=False, clearable=True, disabled=False, style={'display': True}, value='Japan', placeholder='Select Countries', options=[{'label': c, 'value': c} for c in (suicide['country'].unique())]) ], style={'width': '30%', 'margin-left': '35%'}), # Create line chart (show suicides no. by age group) html.Div([ html.Br(), dcc.Graph(id='line_2', config={'displayModeBar': 'hover'}), ],style={'margin-left': '1%','width': '50%', 'display': 'inline-block'}), # Create horizontal bar chart (show top suicides no. by age group) html.Div([ html.Br(), dcc.Graph(id='hor_bar', config={'displayModeBar': 'hover'}), ], style={'margin-right': '6%', 'width': '37.6%', 'display': 'inline-block', 'float': 'right'}), html.Br(), html.Br(), html.Br(), html.Br() ], style={'background-color': '#e6e6e6'}) # Create choropleth map chart @app.callback(Output('map_1', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): suicide1 = suicide.groupby(['country'])['suicides_no'].sum().reset_index() return { 'data': [go.Choropleth( locations=suicide1['country'], z=suicide1['suicides_no'], locationmode='country names', colorscale='portland', showscale=False, autocolorscale=False, reversescale=False, marker=dict(line=dict(color='darkgray', width=0.5)), hoverinfo='text', hovertext= '<b>Country</b>: ' + suicide1['country'].astype(str) + '<br>' + '<b>Suicides</b>: ' + [f'{x:,.0f}' for x in suicide1['suicides_no']] + '<br>' )], 'layout': go.Layout( margin={"r": 0, "t": 0, "l": 0, "b": 0}, width=1820, height=650, plot_bgcolor='#e6e6e6', paper_bgcolor='#e6e6e6', hovermode='closest', geo=dict( showframe=False, showcountries=True, countrycolor='rgb(40,40,40)', showocean=True, oceancolor="LightBlue", showcoastlines=True, coastlinecolor="RebeccaPurple", showland=True, landcolor='rgb(217, 217, 217)', showlakes=True, lakecolor='rgb(85,173,240)', projection={'type': 'equirectangular'} ), ) } # Create line chart (show suicides no. by age group) @app.callback(Output('line_2', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): # Data for line yearly_age = suicide.groupby(['country', 'year', 'age'])['suicides_no'].sum().reset_index() years_5_14 = yearly_age[yearly_age['age'] == '5-14 years'] years_15_24 = yearly_age[yearly_age['age'] == '15-24 years'] years_25_34 = yearly_age[yearly_age['age'] == '25-34 years'] years_35_54 = yearly_age[yearly_age['age'] == '35-54 years'] years_55_74 = yearly_age[yearly_age['age'] == '55-74 years'] years_75_plus = yearly_age[yearly_age['age'] == '75+ years'] return { 'data': [go.Scatter( x=years_5_14[(years_5_14['country'] == w_countries) & (years_5_14['age'] == '5-14 years')]['year'], y=years_5_14[(years_5_14['country'] == w_countries) & (years_5_14['age'] == '5-14 years')]['suicides_no'], name='5-14 years', mode='lines', marker=dict(color='#9C640C'), hoverinfo='text', hovertext= '<b>Country</b>: ' + years_5_14[years_5_14['country'] == w_countries]['country'].astype(str) + '<br>'+ '<b>Year</b>: ' + years_5_14[years_5_14['country'] == w_countries]['year'].astype(str) + '<br>'+ '<b>Age</b>: ' + years_5_14[years_5_14['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in years_5_14[years_5_14['country'] == w_countries]['suicides_no']] + '<br>' ), go.Scatter( x=years_15_24[(years_15_24['country'] == w_countries) & (years_15_24['age'] == '15-24 years')]['year'], y=years_15_24[(years_15_24['country'] == w_countries) & (years_15_24['age'] == '15-24 years')]['suicides_no'], name='15-24 years', mode='lines', marker=dict(color='#ADAF06'), hoverinfo='text', hovertext= '<b>Country</b>: ' + years_15_24[years_15_24['country'] == w_countries]['country'].astype(str) + '<br>'+ '<b>Year</b>: ' + years_15_24[years_15_24['country'] == w_countries]['year'].astype(str) + '<br>'+ '<b>Age</b>: ' + years_15_24[years_15_24['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in years_15_24[years_15_24['country'] == w_countries]['suicides_no']] + '<br>' ), go.Scatter( x=years_25_34[(years_25_34['country'] == w_countries) & (years_25_34['age'] == '25-34 years')]['year'], y=years_25_34[(years_25_34['country'] == w_countries) & (years_25_34['age'] == '25-34 years')]['suicides_no'], name='25-34 years', mode='lines', marker=dict(color='#870379'), hoverinfo='text', hovertext= '<b>Country</b>: ' + years_25_34[years_25_34['country'] == w_countries]['country'].astype(str) + '<br>'+ '<b>Year</b>: ' + years_25_34[years_25_34['country'] == w_countries]['year'].astype(str) + '<br>'+ '<b>Age</b>: ' + years_25_34[years_25_34['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in years_25_34[years_25_34['country'] == w_countries]['suicides_no']] + '<br>' ), go.Scatter( x=years_35_54[(years_35_54['country'] == w_countries) & (years_35_54['age'] == '35-54 years')]['year'], y=years_35_54[(years_35_54['country'] == w_countries) & (years_35_54['age'] == '35-54 years')]['suicides_no'], name='35-54 years', mode='lines', marker=dict(color='#0A0993'), hoverinfo='text', hovertext= '<b>Country</b>: ' + years_35_54[years_35_54['country'] == w_countries]['country'].astype(str) + '<br>'+ '<b>Year</b>: ' + years_35_54[years_35_54['country'] == w_countries]['year'].astype(str) + '<br>'+ '<b>Age</b>: ' + years_35_54[years_35_54['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in years_35_54[years_35_54['country'] == w_countries]['suicides_no']] + '<br>' ), go.Scatter( x=years_55_74[(years_55_74['country'] == w_countries) & (years_55_74['age'] == '55-74 years')]['year'], y=years_55_74[(years_55_74['country'] == w_countries) & (years_55_74['age'] == '55-74 years')]['suicides_no'], name='55-74 years', mode='lines', marker=dict(color='#04B77A'), hoverinfo='text', hovertext= '<b>Country</b>: ' + years_55_74[years_55_74['country'] == w_countries]['country'].astype(str) + '<br>'+ '<b>Year</b>: ' + years_55_74[years_55_74['country'] == w_countries]['year'].astype(str) + '<br>'+ '<b>Age</b>: ' + years_55_74[years_55_74['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in years_55_74[years_55_74['country'] == w_countries]['suicides_no']] + '<br>' ), go.Scatter( x=years_75_plus[(years_75_plus['country'] == w_countries) & (years_75_plus['age'] == '75+ years')]['year'], y=years_75_plus[(years_75_plus['country'] == w_countries) & (years_75_plus['age'] == '75+ years')]['suicides_no'], name='75 years', mode='lines', marker=dict(color='rgb(58, 21, 56)'), hoverinfo='text', hovertext= '<b>Country</b>: ' + years_75_plus[years_75_plus['country'] == w_countries]['country'].astype(str) + '<br>'+ '<b>Year</b>: ' + years_75_plus[years_75_plus['country'] == w_countries]['year'].astype(str) + '<br>'+ '<b>Age</b>: ' + years_75_plus[years_75_plus['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in years_75_plus[years_75_plus['country'] == w_countries]['suicides_no']] + '<br>' )], 'layout': go.Layout( width=1030, height=520, title={ 'text': 'Yearly Suicides : ' + (w_countries), 'y': 0.93, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'}, titlefont={'family': 'Oswald', 'color': 'rgb(230, 34, 144)', 'size': 25}, hovermode='x', xaxis=dict(title='<b>Year</b>', tick0=0, dtick=1, color='rgb(230, 34, 144)', showline=True, showgrid=True, showticklabels=True, linecolor='rgb(104, 204, 104)', linewidth=2, ticks='outside', tickfont=dict( family='Arial', size=12, color='rgb(17, 37, 239)' ) ), yaxis=dict(title='<b>Suicides no.</b>', color='rgb(230, 34, 144)', showline=True, showgrid=True, showticklabels=True, linecolor='rgb(104, 204, 104)', linewidth=2, ticks='outside', tickfont=dict( family='Arial', size=12, color='rgb(17, 37, 239)' ) ), legend=dict(title='', x=0.05, y=1.08, orientation='h', bgcolor='rgba(255, 255, 255, 0)', traceorder="normal", font=dict( family="sans-serif", size=12, color='#000000')), ) } # Create horizontal bar chart (show top suicides no. by age group) @app.callback(Output('hor_bar', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): top_age = suicide.groupby(['country', 'age'])['suicides_no'].sum().reset_index() top_age_country = top_age[top_age['country'] == w_countries].sort_values(by='suicides_no', ascending=False) return { 'data': [go.Bar(x=top_age_country[top_age_country['country'] == w_countries]['suicides_no'], y=top_age_country[top_age_country['country'] == w_countries]['age'], text=top_age_country[top_age_country['country'] == w_countries]['suicides_no'], texttemplate='%{text:,.0f}', textposition='auto', marker=dict(color='#04B77A'), orientation='h', hoverinfo='text', hovertext= '<b>Country</b>: ' + top_age_country[top_age_country['country'] == w_countries]['country'].astype(str) + '<br>' + '<b>Age</b>: ' + top_age_country[top_age_country['country'] == w_countries]['age'].astype(str) + '<br>' + '<b>Suicides #</b>: ' + [f'{x:,.0f}' for x in top_age_country[top_age_country['country'] == w_countries]['suicides_no']] + '<br>' )], 'layout': go.Layout( width=800, height=520, # plot_bgcolor='rgb(250, 242, 242)', # paper_bgcolor='rgb(250, 242, 242)', title={ 'text': 'Top Suicides by Age Group :' + (w_countries), 'y': 0.93, 'x': 0.43, 'xanchor': 'center', 'yanchor': 'top'}, titlefont={'family': 'Oswald', 'color': 'rgb(230, 34, 144)', 'size': 25}, hovermode='closest', margin=dict(l=110), xaxis=dict(title='<b>Suicides no.</b>', color='rgb(230, 34, 144)', showline=True, showgrid=True, showticklabels=True, linecolor='rgb(104, 204, 104)', linewidth=2, ticks='outside', tickfont=dict( family='Arial', size=12, color='rgb(17, 37, 239)' ) ), yaxis=dict(title='<b>Age Group</b>', autorange='reversed', color='rgb(230, 34, 144)', showline=True, showgrid=False, showticklabels=True, linecolor='rgb(104, 204, 104)', linewidth=2, ticks='outside', tickfont=dict( family='Arial', size=12, color='rgb(17, 37, 239)' ) ) ) } if __name__ == '__main__': app.run_server(debug=True) |