【问题标题】:How to specify n-th ticker for bokeh plot from python side, where n is the number of tickers如何从 python 端为散景图指定第 n 个股票代码,其中 n 是股票代码的数量
【发布时间】:2023-03-23 20:02:01
【问题描述】:

维护人员注意: 不推荐使用对 Coffeescript 的支持,并将在 Bokeh 2.0 中删除。



前几天,我问了如何控制股票:

How to show only evey nth categorical tickers in Bokeh

现在我需要从 Coffeescript 外部提供 n,其中 n 是每 n 个股票代码。

基于 bigreddot 提供的代码 sn-p 并参考以下链接显示的代码示例:

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?

尽管我不了解 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”似乎不起作用。结果为空白。

【问题讨论】:

    标签: python plot bokeh ticker


    【解决方案1】:

    除非我记错了,你只需要访问nth作为一个实例变量,把this.放在它前面。

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

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

    【讨论】:

    • 感谢 Bryan 的更新。不幸的是,'this.nth' 不起作用。我只是放了一个完整的代码sn-p马上复制运行。
    • Bryan,感谢您抽出宝贵时间回答我的问题。现在我最好花点时间自己学习 Coffeescript!:)
    猜你喜欢
    • 2011-01-23
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2021-03-02
    • 2023-03-29
    相关资源
    最近更新 更多