CarlaVdB
CarlaVdB

Reputation: 315

Bokeh colorbar customisation

Good day,

I have the following colorbar for my Bokeh chart. following colorbar

My code for the colorbar is as follows:

palette = brewer['Greys'][8]
palette = palette[::-1]

formatter = PrintfTickFormatter(format='%1.0f')

color_mapper = LinearColorMapper(palette = palette, low = 0, nan_color = '#d9d9d9')

color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20,
        border_line_color=None,location = (0,0), orientation = 'horizontal', formatter = formatter)

I would like to make a couple of changes and was wondering if anyone can assist me with any of the following:

  1. Add a border to the colorbar so that one can see where "0" starts,
  2. Get the tick marks to line up with the colors,
  3. Have the final value of the colorbar included (at the very end of the bar there needs to be an ending value,
  4. Change the format of the ticks from e.g. 200000 to 200k.

Thank you to anyone who can assist me! Have a great day.

Upvotes: 1

Views: 1273

Answers (1)

mosc9575
mosc9575

Reputation: 6367

Here is a collection:

  1. ColorBar has a bar_line_color. Default is None. Set this to your favorite color (like "black")
  2. ColorBar has a ticker and you can set this to BasicTicker(desired_num_ticks=len(palette)+1).
  3. Use the NumeralTickFormatter with the format (0.00 a) to display numbes i the wanted format

See the example below:

from bokeh.palettes import brewer
from bokeh.transform import transform
from bokeh.models import NumeralTickFormatter ,LinearColorMapper, ColorBar, BasicTicker, ColumnDataSource
from bokeh.plotting import figure, show, output_notebook
output_notebook()

source = ColumnDataSource(dict(
    x=[1,10,100,200,400,1000,2000,4000],
    y=[1,2,3,4,5,6,7,8]
))

palette = brewer['Greys'][8][::-1]

p = figure(width=500, height=300)
color_mapper = LinearColorMapper(palette = palette, low = 0, nan_color = '#d9d9d9')
p.circle('x', 'y', color=transform("x", color_mapper), line_color='black', size=10, source=source)

color_bar = ColorBar(
    color_mapper=color_mapper, 
    label_standoff=8,
    width = 425,
    height = 20,
    location = (0,0),
    orientation = 'horizontal',
    formatter = NumeralTickFormatter(format='0.00 a'),
    ticker=BasicTicker(desired_num_ticks=len(palette)+1),
    bar_line_color='black',
    major_tick_line_color='black'
)
p.add_layout(color_bar, 'below')
show(p)

plot with color mapper

Upvotes: 1

Related Questions