Bokeh Clustered bar chart

enter image description hereI have some data:

data = {'FFCB' : ['D','I'],
        'CS'   : [0.966248, 0],
        'FPSI'   : [0.00264871, 0],
        'SA'   : [0, 0.114216],
        'NA'   : [0.0127895, 0.00567031],
        'O'   : [0.00552444, 0],
        'FPSDA'   : [0.00136219, 0],
        'HDR'   : [3.78387e-05, 0]}

I would like to create a clustered bar chart in bokeh, such that the value of the bar and its x label is shown in the tooltip. Also the xaxis major label should be shown in the legend. xaxis major label is not shown. Only the group label is shown. xaxis subgroup label are the label values ('D','I'). xaxis major label are the balance labels. ('CS','FPSI','SA','NA','O','FPSDA','HDR'). The major lables need to be shown on the legend since they are long in the real data (As shown in the image). The value of the bar should be shown in the tooltip.

Would anyone be able help me on this matter. Please see image.

Thanks

Michael

Upvotes: 0

Views: 735

Answers (1)

Tony
Tony

Reputation: 8287

This should do the job (tested for Bokeh v1.0.4). You can change the legend orientation to "vertical" if you like.

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, HoverTool, CustomJS
from bokeh.plotting import figure
from bokeh.transform import dodge
from bokeh.palettes import Spectral6

data = {'FFCB' : ['D', 'I'],
        'CS'   : [0.013254, 0.01],
        'FPSI'   : [0.00264871, 0.02],
        'SA'   : [0.03, 0.114216],
        'NA'   : [0.0127895, 0.00567031],
        'O'   : [0.00552444, 0.03],
        'FPSDA'   : [0.00136219, 0.04],
        'HDR'   : [0.03, 0.05]}

source = ColumnDataSource(data = data)

p = figure(x_range = data['FFCB'], y_range = (0, 0.2), plot_width = 600, plot_height = 400, title = "Clustered bar chart", tools = '')

vbar1 = p.vbar(x = dodge('FFCB', -0.25, range = p.x_range), top = 'CS', width = 0.1, source = source,
       color = Spectral6[0], legend = value("CS"))
hover_tool_vbar1 = HoverTool(tooltips = [('CS', '@CS{0.000}')], show_arrow = False, renderers = [vbar1])

vbar2 = p.vbar(x = dodge('FFCB', -0.15, range = p.x_range), top = 'FPSI', width = 0.1, source = source,
       color = Spectral6[1], legend = value("FPSI"))
hover_tool_vbar2 = HoverTool(tooltips = [('FPSI', '@FPSI{0.000}')], show_arrow = False, renderers = [vbar2])

vbar3 = p.vbar(x = dodge('FFCB', -0.05, range = p.x_range), top = 'SA', width = 0.1, source = source,
       color = Spectral6[2], legend = value("SA"))
hover_tool_vbar3 = HoverTool(tooltips = [('SA', '@SA{0.000}')], show_arrow = False, renderers = [vbar3])

vbar4 = p.vbar(x = dodge('FFCB', 0.05, range = p.x_range), top = 'NA', width = 0.1, source = source,
       color = Spectral6[3], legend = value("NA"))
hover_tool_vbar4 = HoverTool(tooltips = [('NA', '@NA{0.000}')], show_arrow = False, renderers = [vbar4])

vbar5 = p.vbar(x = dodge('FFCB', 0.15, range = p.x_range), top = 'O', width = 0.1, source = source,
       color = Spectral6[4], legend = value("O"))
hover_tool_vbar5 = HoverTool(tooltips = [('O', '@O{0.000}')], show_arrow = False, renderers = [vbar5])

vbar6 = p.vbar(x = dodge('FFCB', 0.25, range = p.x_range), top = 'HDR', width = 0.1, source = source,
       color = Spectral6[5], legend = value("HDR"))
hover_tool_vbar6 = HoverTool(tooltips = [('HDR', '@HDR{0.000}')], show_arrow = False, renderers = [vbar6])

p.x_range.range_padding = 0.2
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.click_policy = 'hide'
p.legend.orientation = "horizontal"

p.add_tools(hover_tool_vbar1, hover_tool_vbar2, hover_tool_vbar3, hover_tool_vbar4, hover_tool_vbar5, hover_tool_vbar6)

show(p)

Result:

enter image description here

Upvotes: 1

Related Questions