使用Taptool更新散景图中的颜色映射器 [英] Updating color mapper in a bokeh plot with taptool

查看:95
本文介绍了使用Taptool更新散景图中的颜色映射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据用户可以选择的变量来更改多边形的颜色图.我可以更新颜色,但是如果使用TapTool选择一个多边形,则会出现初始颜色图:

I need to change the color map of polygons based on a variable that the user can select. I can update the colors, but if I select one polygon with TapTool then the initial color map appears:

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column, layout
from bokeh.models import ColumnDataSource, LinearColorMapper, ColorBar, BasicTicker, Select
from bokeh.palettes import Viridis256 as palette

palette.reverse()
TOOLS = "tap"
p = figure(title="Coloring Humidity", tools=TOOLS)

source = ColumnDataSource(dict(x=[[1, 3, 2], [3, 4, 6, 6]],
                               y=[[2, 1, 4], [4, 7, 8, 5]],
                               name=['A', 'B'],
                               humidity=[0, 1.0],
                               temperature=[10.0, 0.0]
                               )
                          )

color_mapper = LinearColorMapper(palette=palette, low=0, high=1)

pglyph = p.patches('x', 'y', source=source, fill_color={'field': 'humidity', 'transform': color_mapper},
                   alpha=1, line_width=2)

color_bar = ColorBar(color_mapper=color_mapper, label_standoff=12, border_line_color=None, location=(0, 0),
                     ticker=BasicTicker())
p.add_layout(color_bar, 'left')


def color_change(attr, old, new):
    cm = p.select_one(LinearColorMapper)
    if new == 'humidity':
        cm.update(low=0, high=1.0)
    elif new == 'temperature':
        cm.update(low=0, high=10)
    else:
        raise ValueError('unknown color')
    pglyph.glyph.fill_color['field'] = new
    p.title.text = 'Coloring {}'.format(new.title())


select = Select(value='humidity', options=['humidity', 'temperature'])
select.on_change('value', color_change)

l = layout([
    [select],
    [p]
])

curdoc().add_root(l)

在此脚本中,如果我在选择小部件中选择温度",颜色会重新映射而不会出现问题,但是如果我随后使用taptool选择了一个多边形,则选定和未选定多边形的颜色将返回到颜色映射.湿度.我想我缺少了一些东西,但我不知道是什么.

In this script, if I select 'temperature' in the select widget, the colors remap without problem, but if I then select one of the polygons with taptool, the color of the selected and unselected polygons go back to the color mapping of humidity. I guess i'm missing something but i can't tell what.

推荐答案

我认为您只需要删除以下行:

I think you just have to remove the line:

pglyph.glyph.fill_color['field'] = new

在回调中更改ColorMapper的低/高属性应该会使数据重新进行颜色映射,因此您不必手动设置颜色.

Changing the low/high properties of the ColorMapper in your callback should cause the data to re-color mapped, so you shouldn't have to set the colors manually.

这篇关于使用Taptool更新散景图中的颜色映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆