在bokeh的标签和颜色纵的沟纹

时间:2016-12-02 13:55:32

标签: python-3.x widget label bokeh

我正在尝试散景。到目前为止,它非常有趣。但我并不完全了解它。我的目标是制作一个简单但交互式的散点图。

我有三个主要问题:

  1. 我想用names
  2. 标记散点图
  3. 我希望根据colors
  4. 对散点图进行着色
  5. 我很喜欢小工具,我可以决定是否显示颜色和名称。
  6. 这是我到目前为止所做的。我试图使用LabelSet,但我被困住了。任何帮助是极大的赞赏!

    # interactive widget bokeh figure
    from bokeh.io import curdoc
    from bokeh.layouts import row, widgetbox
    from bokeh.models import ColumnDataSource
    from bokeh.models.widgets import Slider, TextInput
    from bokeh.plotting import figure
    from bokeh.models import Range1d, LabelSet, Label
    import numpy as np
    
    # data
    x = [-4, 3, 2, 4, 10, 11, -2, 6]
    y = [-3, 2, 2, 9, 11, 12, -5, 6]
    names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
    colors =['r', 'y', 'y', 'r', 'g', 'g', 'g', 'g']
    
    
    
    p = figure(plot_height=400, plot_width=400, title="a little interactive chart",         
                    tools="crosshair,pan,reset,save,wheel_zoom",
                    x_range=[-10, 10], y_range=[-10, 10])
    
    labels = LabelSet(x='x', y='y', text='names', level='glyph',
             x_offset=5, y_offset=5)
    
    p.add_layout(labels)
    
    
    p.circle(x, y, fill_color="red", line_color="red", size=6)
    
    # Set up widgets
    text = TextInput(title="title", value='a little interavtive chart')
    
    # Set up callbacks
    def update_title(attrname, old, new):
        p.title.text = text.value
    
    text.on_change('value', update_title)
    
    # # Set up layouts and add to document
    inputs = widgetbox(text, names)
    
    curdoc().add_root(row(inputs, p, width=800))
    curdoc().title = "Sliders"
    

1 个答案:

答案 0 :(得分:1)

通常,您使用LabelSet来使用与某些字形渲染器相同的数据源进行配置。我发现无论何时共享列数据源,最好还是明确地创建它们。以下是呈现代码的更新版本:

# interactive widget bokeh figure
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource, Range1d, LabelSet, Label
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure

# data
x = [-4, 3, 2, 4, 10, 11, -2, 6]
y = [-3, 2, 2, 9, 11, 12, -5, 6]
names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
colors =['r', 'y', 'y', 'r', 'g', 'g', 'g', 'g']

# create a CDS by hand
source = ColumnDataSource(data=dict(x=x, y=y, names=names, colors=colors))

p = figure(plot_height=400, plot_width=400, title="a little interactive chart",
           tools="crosshair,pan,reset,save,wheel_zoom",
           x_range=[-10, 10], y_range=[-10, 10])

# pass the CDS here, and column names (not the arrays themselves)
p.circle('x', 'y', fill_color="red", line_color="red", size=6, source=source)

# pass the CDS here too
labels = LabelSet(x='x', y='y', text='names', level='glyph',
         x_offset=5, y_offset=5, source=source)
p.add_layout(labels)

# Set up widgets
text = TextInput(title="title", value='a little interavtive chart')

# Set up callbacks
def update_title(attrname, old, new):
    p.title.text = text.value

text.on_change('value', update_title)

# # Set up layouts and add to document
inputs = widgetbox(text)

curdoc().add_root(row(inputs, p, width=800))
curdoc().title = "Sliders"

我还从names删除了widgetbox,因为小部件框只能包含小部件模型。也许您打算在Select窗口小部件中使用名称?