How to draw and update categorical bar plot using vbar method in Bokeh plotting module

I wish to draw a bar plot using vbar method in Bokeh plotting, where x axis takes categorical values rather than numerical ones.

However, the example provided in the tutorial page (http://bokeh.pydata.org/en/latest/docs/reference/plotting.html#) has only numerical x axis.

The bar plot must be updatable via widget and therefore it seems that Bar() cannot be used so instead I tried using vbar() method, where I can feed source data.

I found several similar questions and answers from the history, but still they don’t seem to exactly address the problem I have.
I tried the following code snippet but it failed with some errors:

from bokeh.plotting import figure, output_file
from bokeh.io import show
from bokeh.models import ColumnDataSource, ranges
from bokeh.plotting import figure
import pandas as pd

output_file(“test_bar_plot.html”)

dat = pd.DataFrame([[‘A’,20],[‘B’,20],[‘C’,30]], columns=[‘category’,‘amount’])

source = ColumnDataSource(dict(x=,y=))

x_label = “Category”
y_label = “Amount”
title = “Test bar plot”

plot = figure(plot_width=600, plot_height=300,
x_axis_label = x_label,
y_axis_label = y_label,
title=title
)

plot.vbar(source=source,x=‘x’,top=‘y’,bottom=0,width=0.3)

def update():
source.data = dict(
x = dat.category,
y = dat.amount
)
plot.x_range = source.data[‘x’] # <===== ERROR

update()

show(plot)

It seems to work if I specify x_range as the figure() argument, but what I want to do is to be able to update categorical values according to widget’s state, in which case, there must be some mechanism by which I can change the x_range on the fly.

Thanks

  • SJ

I got an answer here:

2017년 6월 12일 월요일 오후 9시 56분 56초 UTC+9, Royalblue 님의 말:

···

I wish to draw a bar plot using vbar method in Bokeh plotting, where x axis takes categorical values rather than numerical ones.

However, the example provided in the tutorial page (http://bokeh.pydata.org/en/latest/docs/reference/plotting.html#) has only numerical x axis.

The bar plot must be updatable via widget and therefore it seems that Bar() cannot be used so instead I tried using vbar() method, where I can feed source data.

I found several similar questions and answers from the history, but still they don’t seem to exactly address the problem I have.
I tried the following code snippet but it failed with some errors:

from bokeh.plotting import figure, output_file
from bokeh.io import show
from bokeh.models import ColumnDataSource, ranges
from bokeh.plotting import figure
import pandas as pd

output_file(“test_bar_plot.html”)

dat = pd.DataFrame([[‘A’,20],[‘B’,20],[‘C’,30]], columns=[‘category’,‘amount’])

source = ColumnDataSource(dict(x=,y=))

x_label = “Category”
y_label = “Amount”
title = “Test bar plot”

plot = figure(plot_width=600, plot_height=300,
x_axis_label = x_label,
y_axis_label = y_label,
title=title
)

plot.vbar(source=source,x=‘x’,top=‘y’,bottom=0,width=0.3)

def update():
source.data = dict(
x = dat.category,
y = dat.amount
)
plot.x_range = source.data[‘x’] # <===== ERROR

update()

show(plot)

It seems to work if I specify x_range as the figure() argument, but what I want to do is to be able to update categorical values according to widget’s state, in which case, there must be some mechanism by which I can change the x_range on the fly.

Thanks

  • SJ