Python:如何更新bokeh补丁字段?

时间:2019-06-06 15:24:56

标签: python bokeh

我正在使用bokeh,我想更新补丁的field值。

首先,我将地理数据框gdf转换为GeoJSONDataSource

from bokeh.models import LogColorMapper
from bokeh.palettes import Viridis256 as palette
from bokeh.models.glyphs import Patch
from bokeh.models import GeoJSONDataSource, Select

geo_gdf   = GeoJSONDataSource(geojson=gdf.to_json())

w = 200
h = 200
field = 'col1' ## choose col1 as field to plot
vmin = min(gdf[field])
vmax = max(gdf[field])
## create plot
p = figure(plot_width=w,plot_height=h)
color_mapper = LogColorMapper(palette=palette, low=vmin, high=vmax)

## Patches definition  
grid=p.patches('xs', 'ys', source = geo_source2, name="grid",
          fill_color={'field': field, 'transform': color_mapper},
          fill_alpha=0.5,  line_color="gray", line_width=2)

我想通过选择一个选项来更改field

select =  Select(title="Options",  options = ['Col1', 'Col2'], value = 'Col1')

def update_plot(attrname, old, new):
    option   = select.value
    if option == 'Col1':
        p.field = 'Col1'
    if option == 'Col2':
        p.field = 'Col2'

select.on_change('value', update_plot) 
curdoc().add_root(select)

1 个答案:

答案 0 :(得分:1)

field不是字形的属性,它是如何处理特定属性(例如fill_color)的规范。您将需要使用设置方法进行更新:

grid.glyph.fill_color = {'field': option, 'transform': color_mapper}