Turning off Tick Marks in Bokeh

daedalus picture daedalus · Nov 26, 2014 · Viewed 8.9k times · Source

I am working on the Bokeh (0.6.1) tutorial and trying to turn off the tick marks and labels in one of the exercise plots, the scatter plot:

from __future__ import division

import numpy as np
from six.moves import zip
from bokeh.plotting import *
from bokeh.objects import Range1d

output_file("scatter.html")

figure()

N = 4000

x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
    "#%02x%02x%02x" % (r, g, 150) 
    for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))
]

circle(x, y, radius=radii,
       fill_color=colors, fill_alpha=0.6,
       line_color=None, Title="Colorful Scatter")

grid().grid_line_color = None
axis().axis_line_color = None

# QUESTION PART 1: Is this the right way to turn off tick labels?
axis().major_label_text_font_size = '0pt'  
# QUESTION PART 2: ...and how to turn off tick marks also?

show()  # open a browser

I have managed to turn off the tick labels but no amount of searching the documentation and googling has revealed the incantation needed to turn off the tick marks.

Also I am not sure that setting axis().major_label_text_font_size to 0pt is the right way to turn off tick labels or if it is a kludge. Nothing else seemed to work.

Am I missing something obvious?

Answer

Steven C. Howell picture Steven C. Howell · Feb 6, 2017

This answer is an update for the more recent 0.12.4 version of Bokeh. For additional information, these commands are taken from the Styling Visual Attributes page of the Bokeh documentation.

To turn off the major and minor tick marks set their color to None:

p = bokeh.plotting.figure(plot_width=400, plot_height=400)
p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)

p.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks

p.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks

To turn off the tick labels set the font size to '0pt':

p.xaxis.major_label_text_font_size = '0pt'  # turn off x-axis tick labels
p.yaxis.major_label_text_font_size = '0pt'  # turn off y-axis tick labels

A similar result can be achieved by setting the font color to `None', with the disadvantage that space is still maintained for the tick labels.

p.xaxis.major_label_text_color = None  # turn off x-axis tick labels leaving space
p.yaxis.major_label_text_color = None  # turn off y-axis tick labels leaving space 

This code snippet exemplifies removing both the major and minor tick lines and also the tick labels.

import bokeh.io
import bokeh.plotting
import bokeh.layouts
bokeh.io.output_file('remove_tick_marks.html')

p0 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='original')
p0.circle([1,2,3,4,5], [2,5,8,2,7], size=10)

p1 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='remove tick marks')
p1.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
p1.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p1.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
p1.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p1.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks

p2 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='remove tick labels')
p2.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
p2.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p2.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
p2.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p2.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks
p2.xaxis.major_label_text_font_size = '0pt'  # preferred method for removing tick labels
p2.yaxis.major_label_text_font_size = '0pt'  # preferred method for removing tick labels

p3 = bokeh.plotting.figure(plot_width=200, plot_height=200, 
                           x_axis_label='x', y_axis_label='y', 
                           title='notice extra space')
p3.circle([1,2,3,4,5], [2,5,8,2,7], size=10)
p3.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
p3.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
p3.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
p3.yaxis.minor_tick_line_color = None  # turn off y-axis minor ticks
p3.xaxis.major_label_text_color = None  #note that this leaves space between the axis and the axis label  
p3.yaxis.major_label_text_color = None  #note that this leaves space between the axis and the axis label  

grid = bokeh.layouts.gridplot([[p0, p1, p2, p3]])
bokeh.io.show(grid)

enter image description here