2017-03-06 35 views
0

沒有人知道是否/如何使用Bokeh服務器使用「自定義」功能在Bokeh中繪圖?例如,我知道你可以使用類似使用自定義函數在散景中繪圖?

plot = figure(toolbar_location=None) 
plot.vbar(x='x', width=0.5, bottom=0, top='y', source=source) 

但你怎麼能陰謀使用類似

def mplot(source): 
    p = pd.DataFrame() 
    p['aspects'] = source.data['x'] 
    p['importance'] = source.data['y'] 
    plot = Bar(p, values='importance', label='aspects', legend=False) 
    return plot 

我現在的嘗試是,在這裏:

http://pastebin.com/7Zk9ampq

但不運行。我並不擔心獲取函數「update_samples_or_dataset」的工作,只是最初顯示的情節。任何幫助將非常感激。謝謝!

回答

0

我認爲你仍然需要將你的Bar實例附加到Figure實例;一個Figure是一組情節,基本上,像工具欄一樣精細。

1

這是你想要的嗎?請注意,我沒有使用從bokeh.charts導入的Bar函數,因爲它在更新數據源時不會更新。 如果您想堅持使用來自bokeh.charts的Bar,則需要每次重新創建繪圖。

注意:要運行此程序並進行更新工作 - 您需要從命令行執行bokeh serve --show plotfilename.py

from bokeh.io import curdoc 
from bokeh.layouts import layout 
from bokeh.models.widgets import Button 
from bokeh.plotting import ColumnDataSource, figure 
import random 

def bar_plot(fig, source): 
    fig.vbar(x='x', width=0.5, bottom=0,top='y',source=source, color="firebrick") 
    return fig 

def update_data(): 
    data = source.data 
    data['y'] = random.sample(range(0,10),len(data['y'])) 
    source.data =data 

button = Button(label="Press here to update data", button_type="success") 
button.on_click(update_data) 
data = {'x':[0,1,2,3],'y':[10,20,30,40]} 
source = ColumnDataSource(data) 
fig = figure(plot_width=650, 
      plot_height=500, 
      x_axis_label='x', 
      y_axis_label='y') 
fig = bar_plot(fig, source) 
layout = layout([[button,fig]]) 
curdoc().add_root(layout) 

編輯:請參閱下方繪製一個背景虛化的情節,從數據幀使用的數據,你想要的方法。它也將更新每個按鈕按下的情節。仍然需要使用命令bokeh serve --show plotfilename.py

from bokeh.io import curdoc 
from bokeh.layouts import layout 
from bokeh.models.widgets import Button 
from bokeh.plotting import ColumnDataSource 
from bokeh.charts import Bar 
import random 
import pandas as pd 

def bar_plot(source): 
    df = pd.DataFrame(source.data) 
    fig = Bar(df, values='y', color="firebrick") 
    return fig 

def update_data(): 
    data = {'x':[0,1,2,3],'y':random.sample(range(0,10),4)} 
    source2 = ColumnDataSource(data) 
    newfig = bar_plot(source2) 
    layout.children[0].children[1] = newfig 

button = Button(label="Press here to update data", button_type="success") 
button.on_click(update_data) 
data = {'x':[0,1,2,3],'y':[10,20,30,40]} 
source = ColumnDataSource(data) 
fig = bar_plot(source) 
layout = layout([[button,fig]]) 
curdoc().add_root(layout)