如何从 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 中删除。
前几天,我问如何控制行情:
现在我需要从 Coffeescript 外部提供 n,其中 n 是每第 n 个代码。
基于 bigreddot 提供的代码片段并参考以下链接中显示的代码示例:
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" 好像不行。结果是空白。
除非我弄错了,否则您只需要将 nth
作为实例变量访问,方法是将 this.
放在它前面。
编辑:您还需要在过滤器中使用 "fat arrow" =>
,以便正确绑定 this
(否则 this
在该上下文中未定义) .
ticks.major = ticks.major.filter((element, index) => index % this.nth == 0)
维护者注意事项:对 Coffeescript 的支持已弃用,并将在 Bokeh 2.0 中删除。
前几天,我问如何控制行情:
现在我需要从 Coffeescript 外部提供 n,其中 n 是每第 n 个代码。
基于 bigreddot 提供的代码片段并参考以下链接中显示的代码示例:
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" 好像不行。结果是空白。
除非我弄错了,否则您只需要将 nth
作为实例变量访问,方法是将 this.
放在它前面。
编辑:您还需要在过滤器中使用 "fat arrow" =>
,以便正确绑定 this
(否则 this
在该上下文中未定义) .
ticks.major = ticks.major.filter((element, index) => index % this.nth == 0)