Search code examples
pythonbokehticker

How to show only evey nth categorical tickers in Bokeh


There was the same question two years ago. It seemed that evey nth categorical tickers was not supported at that time.

https://stackoverflow.com/questions/34949298/python-bokeh-show-only-every-second-categorical-ticker

My bokeh version is 0.12.13. I wonder it is supported now.

Simply setting p.xaxis.ticker = ['A', 'B, 'C'] does not work(error is thrown)

In my dashbaord, the initial plot size is one quarter of browser view port and the x axis is crowded with many ticker and labels. So I want to show only 10 tickers and later show all of them when the plot is enlarged.


Solution

  • There's nothing built in to Bokeh to do this. You could accomplish something with a custom extension:

    from bokeh.models CategoricalTicker
    
    JS_CODE = """
    import {CategoricalTicker} from "models/tickers/categorical_ticker"
    
    export class MyTicker extends CategoricalTicker
      type: "MyTicker"
    
      get_ticks: (start, end, range, cross_loc) ->
        ticks = super(start, end, range, cross_loc)
    
        # drops every other tick -- update to suit your specific needs
        ticks.major = ticks.major.filter((element, index) -> index % 2 == 0)
    
        return ticks
    
    """
    
    class MyTicker(CategoricalTicker):
        __implementation__ = JS_CODE
    
    p.xaxis.ticker = MyTicker()
    

    Note that the simple get_ticks defined above will not handle more complicated situations with nested categories, etc.