-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapminder_with_bokeh.py
240 lines (168 loc) · 6.58 KB
/
gapminder_with_bokeh.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
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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import pandas as pd
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, CategoricalColorMapper
from bokeh.models import Slider, HoverTool, Select
from bokeh.palettes import Spectral6
from bokeh.layouts import row, widgetbox, column
# Import Gapminder data
data = pd.read_csv("gapminder_tidy.csv", index_col="Year")
# Make the ColumnDataSource: source
source = ColumnDataSource(data={
'x' : data.loc[1970].fertility,
'y' : data.loc[1970].life,
"country" : data.loc[1970].Country,
"pop" : (data.loc[1970].population / 20000000) + 2,
"region" : data.loc[1970].region,
})
# Save the minimum and maximum values of the fertility column: xmin, xmax
xmin, xmax = min(data.fertility), max(data.fertility)
# Save the minimum and maximum values of the life expectancy column: ymin, ymax
ymin, ymax = min(data.life), max(data.life)
# Create the figure: plot
plot = figure(title='Gapminder Data for 1970', plot_height=400, plot_width=700,
x_range=(xmin, xmax), y_range=(ymin, ymax))
# Set the labels
plot.xaxis.axis_label ='Fertility (children per woman)'
plot.yaxis.axis_label = 'Life Expectancy (years)'
# Make a color mapper: color_mapper
regions_list = data.region.unique().tolist()
color_mapper = CategoricalColorMapper(factors=regions_list, palette=Spectral6)
# Add the color mapper to the circle glyph
plot.circle(x='x', y='y', fill_alpha=0.8, source=source,
color=dict(field="region", transform=color_mapper), legend="region")
plot.legend.location = 'bottom_left'
# Create a HoverTool: hover
hover = HoverTool(tooltips=[("Country","@country")])
# Add the HoverTool to the plot
plot.add_tools(hover)
# Define the callback: update_plot
def update_plot(attr, old, new):
# Read the current value off the slider and 2 dropdowns: yr, x, y
yr = slider.value
x = x_select.value
y = y_select.value
# Label axes of plot
plot.xaxis.axis_label = x
plot.yaxis.axis_label = y
# Set new_data
new_data = {
'x' : data.loc[yr][x],
'y' : data.loc[yr][y],
'country' : data.loc[yr].Country,
'pop' : (data.loc[yr].population / 20000000) + 2,
'region' : data.loc[yr].region,
}
# Assign new_data to source.data
source.data = new_data
# Set the range of all axes
plot.x_range.start = min(data[x])
plot.x_range.end = max(data[x])
plot.y_range.start = min(data[y])
plot.y_range.end = max(data[y])
# Add title to plot
plot.title.text = 'Gapminder data for %d' % yr
# Create a dropdown slider widget: slider
slider = Slider(start=1970, end=2010, step=1, value=1970, title='Year')
# Attach the callback to the 'value' property of slider
slider.on_change('value', update_plot)
# Create a dropdown Select widget for the x data: x_select
x_select = Select(
options=['fertility', 'life', 'child_mortality', 'gdp'],
value='fertility',
title='x-axis data'
)
# Attach the update_plot callback to the 'value' property of x_select
x_select.on_change('value', update_plot)
# Create a dropdown Select widget for the y data: y_select
y_select = Select(
options=['fertility', 'life', 'child_mortality', 'gdp'],
value='life',
title='y-axis data'
)
# Attach the update_plot callback to the 'value' property of y_select
y_select.on_change("value", update_plot)
# Create layout and add to current document
layout = row(widgetbox(slider, x_select, y_select), plot)
curdoc().add_root(layout)
#--------------------------------------------------------
## Make the ColumnDataSource: source
#source = ColumnDataSource(data={
#'x' : data.loc[1970].fertility,
#'y' : data.loc[1970].life,
#'country' : data.loc[1970].Country,
#})
## Create the figure: p
#p = figure(title='1970', x_axis_label='Fertility (children per woman)', y_axis_label='Life Expectancy (years)',
#plot_height=400, plot_width=700,
#tools=[HoverTool(tooltips='@country')])
## Add a circle glyph to the figure p
#p.circle(x='x', y='y', source=source)
## Add the plot to the current document and add a title
#curdoc().add_root(plot)
#curdoc().title = 'Gapminder'
#output_file('gapminder.html')
## In[18]:
## Make a list of the unique values from the region column: regions_list
#
## Import CategoricalColorMapper from bokeh.models and the Spectral6 palette from bokeh.palettes
## Set the legend.location attribute of the plot to 'top_right'
## Add the plot to the current document and add the title
#curdoc().add_root(plot)
#curdoc().title = 'Gapminder'
## In[23]:
## Import the necessary modules
## Define the callback function: update_plot
#def update_plot(attr, old, new):
## Set the yr name to slider.value and new_data to source.data
#yr = slider.value
#new_data = {
#'x' : data.loc[yr].fertility,
#'y' : data.loc[yr].life,
#'country' : data.loc[yr].Country,
#'pop' : (data.loc[yr].population / 20000000) + 2,
#'region' : data.loc[yr].region,
#}
#source.data = new_data
## Make a slider object: slider
#slider = Slider(title="Year", start=1970, end=2010, step=1, value=1970)
## Attach the callback to the 'value' property of slider
#slider.on_change("value", update_plot)
## Make a row layout of widgetbox(slider) and plot and add it to the current document
#layout = column(widgetbox(slider), plot)
#curdoc().add_root(layout)
## In[21]:
## Define the callback function: update_plot
#def update_plot(attr, old, new):
## Assign the value of the slider: yr
#yr = slider.value
## Set new_data
#new_data = {
#'x' : data.loc[yr].fertility,
#'y' : data.loc[yr].life,
#'country' : data.loc[yr].Country,
#'pop' : (data.loc[yr].population / 20000000) + 2,
#'region' : data.loc[yr].region,
#}
## Assign new_data to: source.data
#source.data = new_data
## Add title to figure: plot.title.text
#plot.title.text = 'Gapminder data for %d' % yr
## Make a slider object: slider
#slider = Slider(title="Year", start=1970, end=2010, step=1, value=1970)
## Attach the callback to the 'value' property of slider
#slider.on_change("value", update_plot)
## Make a row layout of widgetbox(slider) and plot and add it to the current document
#layout = row(widgetbox(slider), plot)
#curdoc().add_root(layout)
## In[28]:
## Import HoverTool from bokeh.models
#from bokeh.models import HoverTool
## Create layout: layout
#layout = row(widgetbox(slider),plot)
## Add layout to current document
#curdoc().add_root(layout)
## In[31]: