How to use custom tick shortcuts in Bokeh?

I understand how you specify specific ticks to show in Bokeh, but my question is whether there is a way to assign a specific label for display in comparison with the position. For example,

plot.xaxis[0].ticker=FixedTicker(ticks=[0,1]) 

will only show x-axis labels at 0 and 1, but what if instead of showing 0 and 1, I wanted to show Apple and Orange. Sort of

 plot.xaxis[0].ticker=FixedTicker(ticks=[0,1], labels=['Apple', 'Orange']) 

The histogram will not work for the data I draw. Is there anyway to use custom shortcuts in Bokeh like this?

+10
source share
2 answers

As for even later versions of Bokeh ( 0.12.14 or so), it's even simpler. Corrected ticks can be passed directly as a β€œticker” value, and basic label overrides can be provided to explicitly indicate custom labels for specific values:

 from bokeh.io import output_file, show from bokeh.plotting import figure p = figure() p.circle(x=[1,2,3], y=[4,6,5], size=20) p.xaxis.ticker = [1, 2, 3] p.xaxis.major_label_overrides = {1: 'A', 2: 'B', 3: 'C'} output_file("test.html") show(p) 

enter image description here


NOTE. In the old version below is a link to the bokeh.charts , which has since been deprecated and removed

Starting with recent releases of Bokeh (e.g. 0.12.4 or later), it is now much easier to accomplish with FuncTickFormatter :

 import pandas as pd from bokeh.charts import Bar, output_file, show from bokeh.models import FuncTickFormatter skills_list = ['cheese making', 'squanching', 'leaving harsh criticisms'] pct_counts = [25, 40, 1] df = pd.DataFrame({'skill':skills_list, 'pct jobs with skill':pct_counts}) p = Bar(df, 'index', values='pct jobs with skill', title="Top skills for ___ jobs", legend=False) label_dict = {} for i, s in enumerate(skills_list): label_dict[i] = s p.xaxis.formatter = FuncTickFormatter(code=""" var labels = %s; return labels[tick]; """ % label_dict) output_file("bar.html") show(p) 
+11
source

EDIT : Updated for Bokeh 0.12.5 , but also see a simpler method in another answer.

This worked for me:

 import pandas as pd from bokeh.charts import Bar, output_file, show from bokeh.models import TickFormatter from bokeh.core.properties import Dict, Int, String class FixedTickFormatter(TickFormatter): """ Class used to allow custom axis tick labels on a bokeh chart Extends bokeh.model.formatters.TickFormatte """ JS_CODE = """ import {Model} from "model" import * as p from "core/properties" export class FixedTickFormatter extends Model type: 'FixedTickFormatter' doFormat: (ticks) -> labels = @get("labels") return (labels[tick] ? "" for tick in ticks) @define { labels: [ p.Any ] } """ labels = Dict(Int, String, help=""" A mapping of integer ticks values to their labels. """) __implementation__ = JS_CODE skills_list = ['cheese making', 'squanching', 'leaving harsh criticisms'] pct_counts = [25, 40, 1] df = pd.DataFrame({'skill':skills_list, 'pct jobs with skill':pct_counts}) p = Bar(df, 'index', values='pct jobs with skill', title="Top skills for ___ jobs", legend=False) label_dict = {} for i, s in enumerate(skills_list): label_dict[i] = s p.xaxis[0].formatter = FixedTickFormatter(labels=label_dict) output_file("bar.html") show(p) 

code result

+3
source

All Articles