How to specify n-th ticker for bokeh plot from python side, where n is the number of tickers

笑着哭i 提交于 2019-12-02 06:54:27

问题


Note from maintainers: Support for Coffeescript is deprecated and will be removed in Bokeh 2.0.



The other day, I asked about how to control tickers:

How to show only evey nth categorical tickers in Bokeh

Now I need to supply n from outside of Coffeescript, where n is the every n-th ticker.

Based on the code snippet provided by bigreddot and by referring to the code examples shown below links:

https://docs.bokeh.org/en/latest/docs/user_guide/extensions_gallery/widget.html#userguide-extensions-examples-widget

How do I use custom labels for ticks in Bokeh?

I came up with the following very minimal code although I don't have knowledge about Coffescript. In fact, however, it does not work.

EDIT:

1) Below is the complete code that you can copy and run. 2) Bokeh version: 0.12.13 Python: 3.6.5

from bokeh.core.properties import Float, Instance, Tuple, Bool, Enum, String,Int
from bokeh.models import  TickFormatter, CategoricalTicker
from bokeh.io import show, output_file
from bokeh.plotting import figure

class MyTicker(CategoricalTicker):
    __implementation__ = """
    import {CategoricalTicker} from "models/tickers/categorical_ticker"
    import * as p from "core/properties"

    export class MyTicker extends CategoricalTicker
      type: "MyTicker"

      @define {
        nth: [ p.Int ]
      } 

      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 % this.nth == 0)

        return ticks

    """

    nth = Int(default=2)


output_file("bars.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']

p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
           toolbar_location=None, tools="")

p.vbar(x=fruits, top=[5, 3, 4, 2, 4, 6], width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0

p.xaxis.ticker = MyTicker(nth=2)

show(p)

"this.nth" doesn't seem to work. The result is blank.


回答1:


Unless I am mistaken, you only need to access nth as an instance variable, by putting this. in front of it.

Edit: You also need to use the "fat arrow" => in your filter, so that this is properly bound (otherwise this is undefined in that context).

ticks.major = ticks.major.filter((element, index) => index % this.nth == 0)



来源:https://stackoverflow.com/questions/49663952/how-to-specify-n-th-ticker-for-bokeh-plot-from-python-side-where-n-is-the-numbe

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