簡體   English   中英

如何從python端為散景圖指定第n個股票代碼,其中n是股票代碼的數量

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

維護者注意:對 Coffeescript 的支持已被棄用,並將在 Bokeh 2.0 中刪除



前幾天,我問了如何控制股票代碼:

如何在散景中僅顯示第 n 個分類代碼

現在我需要從 Coffeescript 外部提供 n,其中 n 是每 n 個股票代碼。

基於 bigreddot 提供的代碼片段並參考以下鏈接中顯示的代碼示例:

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

如何在散景中為刻度使用自定義標簽?

盡管我不了解 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”似乎不起作用。 結果是空白。

除非我弄錯了,你只需要通過把this.作為實例變量來訪問nth this. 在它面前。

編輯:您還需要使用“胖箭頭” =>在你的過濾器,讓this正確綁定(否則this是在這種背景下未定義)。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM