How to show only evey nth categorical tickers in Bokeh

我与影子孤独终老i 提交于 2019-11-28 10:18:39

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/49172201/how-to-show-only-evey-nth-categorical-tickers-in-bokeh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!