-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_dash_01.py
63 lines (54 loc) · 1.53 KB
/
06_dash_01.py
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
'''[summary]
Returns:
[type] -- [description]
'''
import pandas as pd
import plotly.graph_objs as go
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash
DF = pd.read_csv('data/gapminderDataFiveYear.csv')
APP = dash.Dash()
YEAR_OPTIONS = []
for year in DF['year'].unique():
YEAR_OPTIONS.append(dict(
label=str(year),
value=year
))
APP.layout = html.Div([
dcc.Graph(id='graph'),
dcc.Dropdown(
id='year-picker',
options=YEAR_OPTIONS,
value=DF['year'].min()
)
])
@APP.callback(
Output('graph', 'figure'),
[Input('year-picker', 'value')]
)
def update_figure(selected_year):
filtered_df = DF[DF['year'] == selected_year]
traces = []
for continent_name in filtered_df['continent'].unique():
df_by_continent = filtered_df[filtered_df['continent'] == continent_name]
traces.append(
go.Scatter(
x=df_by_continent['gdpPercap'],
y=df_by_continent['lifeExp'],
mode='markers',
opacity=0.7,
marker=dict(
size=15
),
name=continent_name
)
)
return dict(data=traces, layout=go.Layout(
title='My Plot',
xaxis=dict(title='GDP Per Cap', type='log'),
yaxis=dict(title='Life Expectancy')
))
if __name__ == '__main__':
APP.run_server()