-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.py
128 lines (115 loc) · 3.78 KB
/
app.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
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
import pandas as pd
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import json
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
df = pd.read_csv('./data/longform.csv', index_col = 0)
app.layout = html.Div([
html.Div([
html.H3('都道府県別人口とGDP,一人当たりGDP', style={
'textAlign': 'center'
}),
html.Div([
dcc.Graph(id = 'scatter-chart',
hoverData = {'points': [{'customdata': '大阪府'}]},
),
dcc.Slider(
id = 'slider-one',
min = df['year'].min(),
max = df['year'].max(),
marks = {i: '{}'.format(i) for i in range(int(df['year'].min()), int(df['year'].max())) if i % 2 == 1},
)
], style={
'display': 'inline-block',
'width': '60%',
}),
html.Div([
dcc.Graph(id='chart-one'),
dcc.Graph(id='chart-two'),
dcc.Graph(id='chart-three'),
],style={
'display': 'inline-block',
'width': '39%'
})
])
])
@app.callback(
dash.dependencies.Output('scatter-chart', 'figure'),
[dash.dependencies.Input('slider-one', 'value')]
)
def update_graph(selected_year):
dff = df[df['year'] == selected_year]
dffper = dff[dff['item']=='pergdp']
dffgdp = dff[dff['item']== 'GDP']
dffpop = dff[dff['item']== 'popu']
return {
'data': [go.Scatter(
x = dffper[dffper['area']==i]['value'],
y = dffgdp[dffgdp['area']==i]['value'],
mode = 'markers',
customdata = [i],
marker={
'size' : dffpop[dffpop['area']==i]['value']/100,
'color': dffpop[dffpop['area']==i]['value']/10000,
},
name=i,
)for i in dff.area.unique()],
'layout': {
'height': 900,
'xaxis': {
'type': 'log',
'title': '都道府県別一人当たりGDP(log scale)',
'range':[np.log(80), np.log(1200)]
},
'yaxis': {
'type':'log',
'title': '都道府県別GDP(log scale)',
'range':[np.log(80), np.log(8000)]
},
'hovermode': 'closest',
}
}
def create_smallChart(dff, area, name):
return {
'data':[go.Scatter(
x = dff['year'],
y = dff['value']
)],
'layout':{
'height': 300,
'title': '{}の{}データ'.format(area, name)
}
}
@app.callback(
dash.dependencies.Output('chart-one', 'figure'),
[(dash.dependencies.Input('scatter-chart', 'hoverData'))]
)
def createGDP(hoverdata):
areaName = hoverdata['points'][0]['customdata']
dff = df[df['area']==areaName]
dff = dff[dff['item'] == 'GDP']
return create_smallChart(dff, areaName, 'GDP')
@app.callback(
dash.dependencies.Output('chart-two', 'figure'),
[(dash.dependencies.Input('scatter-chart', 'hoverData'))]
)
def createPerGDP(hoverdata):
areaName = hoverdata['points'][0]['customdata']
dff = df[df['area']==areaName]
dff = dff[dff['item'] == 'pergdp']
return create_smallChart(dff, areaName, 'pergdp')
@app.callback(
dash.dependencies.Output('chart-three', 'figure'),
[(dash.dependencies.Input('scatter-chart', 'hoverData'))]
)
def createPopu(hoverdata):
areaName = hoverdata['points'][0]['customdata']
dff = df[df['area']==areaName]
dff = dff[dff['item'] == 'popu']
return create_smallChart(dff, areaName, 'popu')
if __name__=="__main__":
app.run_server(debug=True)