【问题标题】:Changing figure layout elements during bokeh serve在散景服务期间更改图形布局元素
【发布时间】:2019-09-24 22:12:03
【问题描述】:

我有一个显示我的数据的热图的十六进制图。为此,我使用一个包含 x,y 信息的 ColumnDataSource,以及此项的值和颜色。另外,我为十六进制图提供了一个颜色条,显示了哪个值将导致哪种颜色的信息。这应该适用于不同的数据集,但是我无法在运行时根据最小值和最大值更新颜色条。

这是一个示例代码(bokeh 提供的简单 myapp 示例),其中每次按下按钮都应添加一个具有不同值的颜色条:

from random import random
from bokeh.layouts import column
from bokeh.models import Button, LinearColorMapper, ColorBar, BasicTicker
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc
from colorcet import CET_L18 as palette

# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None

# add a text renderer to our plot (no data yet)
r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
           text_baseline="middle", text_align="center")
i = 0

ds = r.data_source
# create a callback that will add a number in a random location
def callback():
    global i

    # BEST PRACTICE --- update .data in one step with a new dict
    new_data = dict()
    new_data['x'] = ds.data['x'] + [random()*70 + 15]
    new_data['y'] = ds.data['y'] + [random()*70 + 15]
    new_data['text_color'] = ds.data['text_color'] + [RdYlBu3[i%3]]
    new_data['text'] = ds.data['text'] + [str(i)]
    ds.data = new_data
    color_mapper = LinearColorMapper(palette=palette, low=0, high=1000*i)

    color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(),
                         label_standoff=12, border_line_color=None, location=(0, 0), orientation="horizontal")
    p.add_layout(color_bar, 'below')
    i = i + 1

# add a button widget and configure with the call back
button = Button(label="Press Me")
button.on_click(callback)
#show(p)
# put the button and plot in a layout and add to the document
curdoc().add_root(column(button, p))

但是,在运行时不会添加颜色条。我也试过事先添加它,然后它会显示但不会更新。 如何在运行时添加彩条和/或更新彩条?

【问题讨论】:

  • 请让我给你一个提示:1) 始终提供最小但可运行的代码 周围很少有人愿意付出额外的努力来复制/粘贴一块将您的代码块分段并添加自己所需的导入以最终重建您的完整程序 2) 将您的代码和描述限制并集中在您的问题上 许多程序员在查看代码时会更快地阅读代码并理解功能在代码中,而不是阅读描述您遗漏的代码的文本。我建议您按照本指南编辑您的问题。
  • 我改变了问题,并提供了一个可运行的示例。在第一次写这个问题时,我已经准备好了这个 sn-p,但是我无法重建我的可视化更新将完全停止的错误。我希望当我提供我的原始代码时,也许一些专家能看到我的错误。感谢您花时间阅读它

标签: python bokeh


【解决方案1】:

首先,将布局设置移到按钮的回调之外,并让回调只更新数据。

接下来,使用bokeh.transform 中的linear_cmap 函数构建线性颜色图。此函数为您提供了一个转换器来获取调色板中某个值的颜色。

最后,更新回调中变换的high

完整的源代码

from random import random
from bokeh.layouts import column
from bokeh.models import Button, ColorBar, BasicTicker
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
from colorcet import CET_L18 as palette
from bokeh.transform import linear_cmap

# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None

i = 0

color_mapper = linear_cmap(field_name='text', palette=palette, low=0, high=i)
color_bar = ColorBar(
    color_mapper=color_mapper['transform'], 
    ticker=BasicTicker(),
    label_standoff=12,
    border_line_color='black',
    location=(0, 0),
    orientation="horizontal")

# add a text renderer to our plot (no data yet)
ds = ColumnDataSource(dict(x=[],y=[],text=[]))
r = p.text(x='x', y='y', text='text', text_color=color_mapper, text_font_size="20pt",
           text_baseline="middle", text_align="center", source=ds)
p.add_layout(color_bar, 'below')

# create a callback that will add a number in a random location
def callback():
    global i

    i = i + 1 
    # BEST PRACTICE --- update .data in one step with a new dict
    new_data = dict()
    new_data['x'] = ds.data['x'] + [random()*70 + 15]
    new_data['y'] = ds.data['y'] + [random()*70 + 15]
    new_data['text'] = ds.data['text'] + [i]
    ds.data = new_data
    color_mapper['transform'].high = i

# add a button widget and configure with the call back
button = Button(label="Press Me")
button.on_click(callback)
#show(p)
# put the button and plot in a layout and add to the document
curdoc().add_root(column(button, p))

【讨论】:

  • 这很好用,非常感谢。是否还有一种简单的方法可以在更新时删除彩条?我有一个数据可视化,确实需要栏
  • 您可以在回调中为color_bar 设置visible 属性,例如color_bar.visible = False
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 2019-03-07
相关资源
最近更新 更多