如何从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中将其删除.
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:
现在我需要从Coffeescript外部提供n,其中n是第n个行情自动收录器.
Now I need to supply n from outside of Coffeescript, where n is the every n-th ticker.
基于bigreddot提供的代码段,并参考以下链接所示的代码示例:
Based on the code snippet provided by bigreddot and by referring to the code examples shown below links:
尽管我不了解Coffescript,但我想出了以下非常简单的代码.但是实际上,它不起作用.
I came up with the following very minimal code although I don't have knowledge about Coffescript. In fact, however, it does not work.
1)下面是您可以复制和运行的完整代码. 2)散景版本:0.12.13 Python:3.6.5
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"似乎不起作用.结果为空白.
"this.nth" doesn't seem to work. The result is blank.
推荐答案
除非我没有记错,否则只需将this.
放在其前面即可访问nth
作为实例变量.
Unless I am mistaken, you only need to access nth
as an instance variable, by putting this.
in front of it.
您还需要在过滤器中使用胖箭头" =>
,以便正确绑定this
(否则在该上下文中未定义this
).
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)
这篇关于如何从python端为散景图指定第n个代码,其中n是代码的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!