2016-12-02 49 views
0

我正在嘗試散景。目前爲止非常有趣。但我並不完全掌握它。我的目標是製作一個簡單但交互式的散點圖。散景中的標籤和顏色標誌符號

我有三個主要問題:

  1. 我要標註與names
  2. 散點圖我想分散於根據着色以colors
  3. 我會愛小部件,我可以決定如果顯示顏色和名稱。

這是我到目前爲止所做的。我試圖使用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

通常你用相同的數據源,一些字形渲染器配置它使用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" 

我也刪除nameswidgetbox因爲小部件箱只能包含窗口小部件的模型。也許你打算使用Select小部件或其他的名字?

+0

哇!謝謝!是的,我正在嘗試使用「選擇」。我對散景完全陌生。抱歉發佈這樣的新手問題!儘管如此,我可能還需要一段時間...... – Rachel

+0

不是'fill_color =「red」'覆蓋'colors = colors'嗎?我的想法是使用「顏色」來分別填充字形? – Rachel

+0

啊......我需要通過'''顏色'與'圓圈'!謝謝! – Rachel