1

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.

bigreddot
  • 33,642
  • 5
  • 69
  • 122
Royalblue
  • 639
  • 10
  • 22

1 Answers1

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)

enter image description here

bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • Thank you Bryan for the update. Unfortunately however 'this.nth' doest not work. I just put a complete code snippet to copy & run right away. – Royalblue Apr 10 '18 at 05:56
  • Bryan, thank you for taking the time to answer my question. Now I better invest some time to learn Coffeescript myself!:) – Royalblue Apr 11 '18 at 00:14
  • Oh, how I wish this was a complete solution. I can't work out how you got that graph (I dream of getting one like that and am stuck). Be nice to publish a complete solution, IMHO. – Bernd Wechner Feb 10 '23 at 10:45