如何从python方面为散景图指定第n个自动收报机,其中n是代码的数量

时间:2018-04-05 04:01:57

标签: python plot bokeh ticker

前几天,我询问了如何控制代码:

How to show only evey nth categorical tickers in Bokeh

现在我需要从Coffeescript外部提供n,其中n是每个第n个自动收报机。

根据bigreddot提供的代码段并参考下面显示的代码示例链接:

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

How do I use custom labels for ticks in Bokeh?

我提出了以下非常简单的代码,尽管我不了解Coffescript。事实上,它不起作用。

编辑:

1)下面是您可以复制和运行的完整代码。 2)散景版: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"似乎不起作用。结果是空白的。

1 个答案:

答案 0 :(得分:1)

除非我弄错了,否则您只需将nth作为实例变量访问,方法是将this.放在其前面。

编辑:您还需要在过滤器中使用“胖箭头”=>,以便正确绑定this(否则this在该上下文中未定义。)

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

enter image description here