添加 Bokeh Slider 以按年份可视化 GIS 数据
Adding Bokeh Slider to visualise GIS data by year
我正在尝试可视化从 GIS 导入的数据。大约有 70 个点,每个点都有地名和年份数据以及每个给定年份的值。
name year mean geometry
0 Place 1 2008 105816 POINT (253662.995 663270.013)
1 Place 1 2009 94381 POINT (253662.995 663270.013)
2 Place 1 2010 101280 POINT (253662.995 663270.013)
3 Place 1 2011 86664 POINT (253662.995 663270.013)
4 Place 1 2012 83828 POINT (253662.995 663270.013)
5 Place 1 2013 91433 POINT (253662.995 663270.013)
6 Place 1 2014 90971 POINT (253662.995 663270.013)
7 Place 1 2015 140151 POINT (253662.995 663270.013)
8 Place 1 2016 104499 POINT (253662.995 663270.013)
9 Place 1 2017 110172 POINT (253662.995 663270.013)
10 Place 1 2018 111700 POINT (253662.995 663270.013)
11 Place 2 2008 99176 POINT (262062.995 669070.013)
12 Place 2 2009 101865 POINT (262062.995 669070.013)
13 Place 2 2010 80560 POINT (262062.995 669070.013)
14 Place 2 2011 61915 POINT (262062.995 669070.013)
15 Place 2 2012 74723 POINT (262062.995 669070.013)
16 Place 2 2013 71550 POINT (262062.995 669070.013)
17 Place 2 2014 239955 POINT (262062.995 669070.013)
18 Place 2 2015 93824 POINT (262062.995 669070.013)
19 Place 2 2016 71751 POINT (262062.995 669070.013)
20 Place 2 2017 86586 POINT (262062.995 669070.013)
21 Place 2 2018 74684 POINT (262062.995 669070.013)
22 Place 3 2008 180296 POINT (251662.995 663270.013)
23 Place 3 2009 165689 POINT (251662.995 663270.013)
24 Place 3 2010 175376 POINT (251662.995 663270.013)
我想使用滑块可视化数据以仅显示具有所选年份值的点。
这就是我正在做的
def getPointCoords(row, geom, coord_type):
"""Calculates coordinates ('x' or 'y') of a Point geometry"""
if coord_type == 'x':
return row[geom].x
elif coord_type == 'y':
return row[geom].y
# Calculate x and y coordinates of the points
stations['x'] = stations.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
stations['y'] = stations.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
# Make a copy, drop the geometry column and create ColumnDataSource
st_df = stations.drop('geometry', axis=1).copy()
stsource = ColumnDataSource(st_df)
#colour based on mean value
colormap = LinearColorMapper(palette='Magma256', low=min(stsource.data['mean']),high=max(stsource.data['mean']))
p= figure(plot_height=400, plot_width=400)
p.circle(x="x", y="y", source=stsource,color = {'field': 'mean', 'transform': colormap})
# Define the callback function: update_plot
def update_plot(attr, old, new):
# Set the year name to slider.value and new_data to source.data
year = slider.value
new_data = {
'x' : stsource.data['year'].x,
'y' :stsource.data['year'].y,
'mean' : stsource.data['year'].mean,
}
stsource.data = new_data
# Make a slider object: slider
slider = Slider(title = 'slider', start = 2008, end = 2020, step = 1, value = 2012)
# 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), p)
curdoc().add_root(layout)
这是我得到的结果
我看到了所有的点(很好!)但是当我吸尘器时我也看到了所有的值。
这部分似乎不起作用,但我不明白为什么。
# Define the callback function: update_plot
def update_plot(attr, old, new):
# Set the year name to slider.value and new_data to source.data
year = slider.value
new_data = {
'x' : stsource.data['year'].x,
'y' :stsource.data['year'].y,
'mean' : stsource.data['year'].mean,
}
stsource.data = new_data
请帮忙!
谢谢。
这最终对我有用
# Define the callback function: update_plot
def update_plot(attr, old, new):
# Set the year name to slider.value and new_data to source.data
year = slider.value
stsource.data = st_df[st_df.year == year]
我意识到,由于我的数据的性质,我不需要单独更新 x、y,因为它们始终相同,但只有一个 'mean' 值。
所以对我来说最简单的似乎是根据年份值更新源数据。
我正在尝试可视化从 GIS 导入的数据。大约有 70 个点,每个点都有地名和年份数据以及每个给定年份的值。
name year mean geometry
0 Place 1 2008 105816 POINT (253662.995 663270.013)
1 Place 1 2009 94381 POINT (253662.995 663270.013)
2 Place 1 2010 101280 POINT (253662.995 663270.013)
3 Place 1 2011 86664 POINT (253662.995 663270.013)
4 Place 1 2012 83828 POINT (253662.995 663270.013)
5 Place 1 2013 91433 POINT (253662.995 663270.013)
6 Place 1 2014 90971 POINT (253662.995 663270.013)
7 Place 1 2015 140151 POINT (253662.995 663270.013)
8 Place 1 2016 104499 POINT (253662.995 663270.013)
9 Place 1 2017 110172 POINT (253662.995 663270.013)
10 Place 1 2018 111700 POINT (253662.995 663270.013)
11 Place 2 2008 99176 POINT (262062.995 669070.013)
12 Place 2 2009 101865 POINT (262062.995 669070.013)
13 Place 2 2010 80560 POINT (262062.995 669070.013)
14 Place 2 2011 61915 POINT (262062.995 669070.013)
15 Place 2 2012 74723 POINT (262062.995 669070.013)
16 Place 2 2013 71550 POINT (262062.995 669070.013)
17 Place 2 2014 239955 POINT (262062.995 669070.013)
18 Place 2 2015 93824 POINT (262062.995 669070.013)
19 Place 2 2016 71751 POINT (262062.995 669070.013)
20 Place 2 2017 86586 POINT (262062.995 669070.013)
21 Place 2 2018 74684 POINT (262062.995 669070.013)
22 Place 3 2008 180296 POINT (251662.995 663270.013)
23 Place 3 2009 165689 POINT (251662.995 663270.013)
24 Place 3 2010 175376 POINT (251662.995 663270.013)
我想使用滑块可视化数据以仅显示具有所选年份值的点。
这就是我正在做的
def getPointCoords(row, geom, coord_type):
"""Calculates coordinates ('x' or 'y') of a Point geometry"""
if coord_type == 'x':
return row[geom].x
elif coord_type == 'y':
return row[geom].y
# Calculate x and y coordinates of the points
stations['x'] = stations.apply(getPointCoords, geom='geometry', coord_type='x', axis=1)
stations['y'] = stations.apply(getPointCoords, geom='geometry', coord_type='y', axis=1)
# Make a copy, drop the geometry column and create ColumnDataSource
st_df = stations.drop('geometry', axis=1).copy()
stsource = ColumnDataSource(st_df)
#colour based on mean value
colormap = LinearColorMapper(palette='Magma256', low=min(stsource.data['mean']),high=max(stsource.data['mean']))
p= figure(plot_height=400, plot_width=400)
p.circle(x="x", y="y", source=stsource,color = {'field': 'mean', 'transform': colormap})
# Define the callback function: update_plot
def update_plot(attr, old, new):
# Set the year name to slider.value and new_data to source.data
year = slider.value
new_data = {
'x' : stsource.data['year'].x,
'y' :stsource.data['year'].y,
'mean' : stsource.data['year'].mean,
}
stsource.data = new_data
# Make a slider object: slider
slider = Slider(title = 'slider', start = 2008, end = 2020, step = 1, value = 2012)
# 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), p)
curdoc().add_root(layout)
这是我得到的结果
我看到了所有的点(很好!)但是当我吸尘器时我也看到了所有的值。
这部分似乎不起作用,但我不明白为什么。
# Define the callback function: update_plot
def update_plot(attr, old, new):
# Set the year name to slider.value and new_data to source.data
year = slider.value
new_data = {
'x' : stsource.data['year'].x,
'y' :stsource.data['year'].y,
'mean' : stsource.data['year'].mean,
}
stsource.data = new_data
请帮忙! 谢谢。
这最终对我有用
# Define the callback function: update_plot
def update_plot(attr, old, new):
# Set the year name to slider.value and new_data to source.data
year = slider.value
stsource.data = st_df[st_df.year == year]
我意识到,由于我的数据的性质,我不需要单独更新 x、y,因为它们始终相同,但只有一个 'mean' 值。 所以对我来说最简单的似乎是根据年份值更新源数据。