Skip to content
Advertisement

Bokeh – draw top and right axes

I think this is very straightforward, but I cannot find an answer to it. I am trying to have a bokeh scatter plot with both the top and right axis drawn. I would like to have something like this (generated using gnuplot):

gnuplot

Here is my simple bokeh code:

from bokeh.plotting import figure, output_file, show

output_file("test.html")
p = figure()

p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.axis.major_tick_in = 10
p.axis.major_tick_out = 0
p.axis.minor_tick_in = 5
p.axis.minor_tick_out = 0

p.line([-10,10], [-10,10])
show(p)

Resulting in:

bokeh

So I want to be able to mirror the left/right and top/bottom axes. I’ve tried using LinearAxis() in bokeh without success. By adding the following lines:

p.add_layout(LinearAxis(), 'right')
p.add_layout(LinearAxis(), 'above')

I got close, but not quite there yet:

bokeh2

I did not find a way to remove the numbers on top/right and also have the tic marks inside. Any help is appreciated. Thank you in advance!

UPDATE

I was able to get the desired layout based on the accepted answer:

from bokeh.plotting import figure, output_file, show

output_file("test.html")
p = figure()

p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
p.axis.major_tick_in = 10
p.axis.major_tick_out = 0
p.axis.minor_tick_in = 5
p.axis.minor_tick_out = 0

p.add_layout(LinearAxis(major_label_text_alpha=0,
                        minor_tick_in=5,minor_tick_out=0,
                        major_tick_in=10,major_tick_out=0),'right')
p.add_layout(LinearAxis(major_label_text_alpha=0,
                        minor_tick_in=5,minor_tick_out=0,
                        major_tick_in=10,major_tick_out=0),'above')

p.line([-10,10], [-10,10])
show(p)

bokeh3

Advertisement

Answer

You can specify text attributes of the tick marks when creating the Axis object:

p.add_layout(
    LinearAxis(
       major_label_text_alpha=0
    ),
    'right'
)

Here are the relevant docs: https://docs.bokeh.org/en/latest/docs/reference/models/axes.html

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement