How to create a proportional (pre-define boundaries) colour bar with python bokeh library


I would like to plot a graph using bokeh with a proportional colour bar same as the graph on the left, – have a proportional colour bar, but this was using matplotlib.colors.BoundaryNorm(bounds, cmap.N).

I would like to use bokeh rather than matplotlib, I did not find any normalise tool in bokeh and my current bokeh version looks like the graph on the right – colour bar not proportional at all.

Please see my bokeh version code:

ticker = FixedTicker(ticks=bounds)
bounds = [0, 5, 25, 75, 95,100]
color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size='5pt',
                         label_standoff=6, border_line_color=None, location=(0, 0))

Although I have given ticker boundaries, the colour bar still not proportional.

The reason I use bokeh is that I would like to use it with Django and have the tools option within the bokeh library.

Please give suggestions. Thanks in advance,

Hi @daochu66 As of Bokeh 1.3.4, Bokeh only has linear and log color mappers. There is no built-in notion of a “segmented” color mapper. I think you could effectively accomplish this, though, by creating a larger palette with 100 entries, that provides the “breaks” you want implicitly. E.g.

palette = [ "yellow" ]*5 + ["pink"]*20 + ["red"]*50 + ...

Then this palette could be used with a LinearColorMapper with low, high = (0, 100).

A segmented color mapper would be a reasonable ask for a new feature, and also an excellent self-contained task for a new contributor. Please feel free to make a GitHub issue to discuss it.

As an aside, I also notice you made an SO question about this. If you could delete the question there, so as not to leave lingering unanswered questions on the bokeh tag (or self-answer it, as you prefer) it would be appreciated.

1 Like

Great, thank you Bryan. I will look into the new feature discussion part. Thanks for the git link.

1 Like

problem solved, happy!


Thanks for following up to let us know!