Kyle
Kyle

Reputation: 183

Plotting in Bokeh using Custom Function?

does anyone know if/how one can use a "custom" function to plot in Bokeh using the Bokeh server? For example, I know you can use something like

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

But how can you plot using something like

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

My current attempt is, here:

http://pastebin.com/7Zk9ampq

but it doesn't run. I'm not worried about getting the function "update_samples_or_dataset" working yet, just the initial plot to show. Any help would be much appreciated. Thanks!

Upvotes: 0

Views: 1234

Answers (2)

Anthonydouc
Anthonydouc

Reputation: 3364

Is this what you want? Note that I did not use the Bar function imported from bokeh.charts as this does not update upon updating the data source. If you want to stick with using Bar from bokeh.charts you need to recreate the plot each time.

Note: to run this and have updating work - you need to execute bokeh serve --show plotfilename.py from the command line.

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)

EDIT: See below a method that plots a bokeh plot but uses data from a dataframe as you wanted. It also will update the plot on each button press. Still you need to use the command 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)

Upvotes: 1

pjz
pjz

Reputation: 43057

I think you still have to attach your Bar instance to a Figure instance; a Figure is a set of plots, essentially, with niceties like the toolbar.

Upvotes: 0

Related Questions