首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新Bokeh中的ColorBar

更新Bokeh中的ColorBar
EN

Stack Overflow用户
提问于 2021-03-31 02:13:49
回答 1查看 207关注 0票数 2

我正在尝试更新Bokeh中的ColorBar,标题和颜色本身都不会更新。下面是一个最小的函数示例(Python 3.6.9,Bokeh 2.3.0,Tornado 6.1):

代码语言:javascript
复制
from bokeh.plotting import figure, curdoc
from bokeh.colors import HSL
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, LinearColorMapper, ColorBar, Select
import numpy as np

def colorize(data,reverse=False):
    if reverse:
        return [HSL(h-180,1.0,0.5).to_rgb() for h in data]
    else:
        return [HSL(h,1.0,0.5).to_rgb() for h in data]

def update(attr,old,new):
    if menu.value == 'reverse':
        y = -1 * x
        c = colorize(x,reverse=True)
    else:
        y = 1 * x
        c = colorize(x)
    source.data = {'x':x,'y':y,'c':c}
    #updateColorBar(p.right[0])
    updateColorBar(p._property_values['right'][0])

def updateColorBar(cb):
    if menu.value == 'reverse':
        cb.title = 'reverse'
        u = colorize(np.linspace(0,360,101,endpoint=True),
                reverse=True)
    else:
        cb.title = 'normal'
        u = colorize(np.linspace(0,360,101,endpoint=True))
    newcm = LinearColorMapper(palette=u,low=0.0,high=2*np.pi)
    cb.color_mapper.palette = newcm.palette
    cb.color_mapper.low = newcm.low
    cb.color_mapper.high = newcm.high

# prepare some data
x = np.linspace(0,360,361,endpoint=True)
y = x
c = colorize(x)

source = ColumnDataSource({'x':x,'y':y,'c':c})

# create a new plot with a title and axis labels
p = figure(title="Simple scatter example", x_axis_label="x",
                y_axis_label="y",toolbar_location="above")

# create a plot to show the colors
p.scatter('x', 'y',size=2,color='c',source=source)

u = colorize(np.linspace(0,360,101,endpoint=True))
color_mapper = LinearColorMapper(palette=u,low=0.0,high=2*np.pi)
color_bar = ColorBar(color_mapper=color_mapper,
                     label_standoff=12,
                     title='normal')

p.add_layout(color_bar, 'right')

menu = Select(title='Order',value='normal',
                options=['normal','reverse'])
menu.on_change('value',update)

t = column(children=[menu,p])

# show the results
curdoc().add_root(t)

我正在用bokeh serve --show file_name.py运行这个例子。

当我从菜单中点击'reverse‘时,绘图本身被正确更新,但ColorBar没有改变。我遵循advice here进行了尽可能小的更改,但也许我的更改太小了?我的猜测是,LinearColorMapper不会推动ColorBar的更新,但在ColorBar上更改标题也不起作用。

编辑:通过在更新方法的末尾添加一个print(cbar.title),我可以看到颜色栏的标题正在更新。当我从菜单中选择这两项时,它会从“正常”更改为“反向”,然后再更改回来。看起来ColorBar根本没有被重新绘制。

Edit2:如果我试图完全替换ColorBar,它就会消失。修改更新函数,添加新函数创建ColorBar:

代码语言:javascript
复制
def update(attr,old,new):
    if new == 'reverse':
        y = 360 - 1 * x
        c = colorize(x,reverse=True)
    else:
        y = 1 * x
        c = colorize(x)
    source.data = {'x':x,'y':y,'c':c}
    cbar = p.right[0]
    #updateColorBar(cbar,new)
    p.right[0] = makeColorBar()
    print(p.right[0].visible)

def makeColorBar():
    if menu.value == 'reverse':
        title = 'reverse'
        u = colorize(np.linspace(0,360,101,endpoint=True),True)
    else:
        title = 'normal'
        u = colorize(np.linspace(0,360,101,endpoint=True))
    cm = LinearColorMapper(palette=u,low=0.0,high=2*np.pi)
    return ColorBar(color_mapper=cm,label_standoff=12,
                          title=title)

当我以不同的方式运行它,使用python3 -i file_name.py并询问p.right[0]的身份时,我得到了ColorBar(id='1042', ...)。如果我手动更改menu.value = 'reverse' (True打印到屏幕上)并查看ColorBar的身份,我就会得到ColorBar(id='1057', ...)。新的颜色条似乎没有被推到显示屏上,而旧的颜色条正在被移除。

Edit3:这是Bokeh中的marked as a bug,计划在2.3.2版本中进行更新。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-03 03:10:11

这个bug has been closed,看起来像是version 3.0的一部分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66876346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档