Customize colors on bar plots

Is there any way to customize the colours for individual bars in a bar plot? I would like one bar in a bar plot to be a different colour than the rest of the bars.

Hi blockhart!

There are a few ways you could do this, depending upon what makes that bar special. Here’s a simple example based on value-- if you literally want only one specific bar to be a different color, maybe you want to go by index position?

from bokeh.plotting import figure, show

x = ['a', 'b', 'c', 'd', 'e', 'f']
num_list = [4, 3, 5, 4, 9, 4]

# construct a list of colors based on characteristics of the values in num_list.
color_list = []
for i in num_list:
    if i > 7:
        color_list.append('orange')
    else:
        color_list.append('blue')

p = figure(x_range=x, plot_height=350, title="vbar color demo",
           toolbar_location=None, tools="")
p.vbar(x=x, top=num_list, color=color_list, width=.9)

show(p)

Thanks for the answer! I should have said I’m using the VBar function not vbar, similar to this example VBar — Bokeh 2.4.2 Documentation. Any recommendations on how to accomplish your above solution with VBar or must I switch to vbar.

Since it looks like VBar takes only a single value as color, any workarounds I’m thinking of would probably be ungainly and cause maintenance issues-- like splitting your data source into two data sources, and then having two different colored glyphs. That would either break or multiply some other things like hover tools, though, and it doesn’t feel like the right way to go.

If it were me, I’d rework to use vbar, unless there’s some other advantage to VBar. I’d love to hear from others in case there’s an approach I’m not thinking of, though.

The glyph methods (e.g. vbar) are a convenience API built on top of the low-level glyph classes (e.g. VBar) There’s nothing you can do with one that you can’t do with the other, but using the low-level classes will be more work and more verbose, which is why I generally recommend folks to use the glyph methods on figure unless there is a specific reason not to.

To use the VBar, you need to:

  • Create a ColumnDataSource with the data columns you want (x, y, colors, etc)
  • Configure the a VBar instance to refer to those column names
  • Explicity add the glyph and the data source together to a plot with add_glyph(...)

There are lots of examples of these steps in the scripts in the examples/models directory.