编辑:由于@tmwilson26 26,我能够使用
javascript code修复它(请参阅下面的注释)。但是,我仍然想知道是否有使用from_py_func的解决方案。
我正在使用Bokeh,并且很难用FuncTickFormatter格式化我的轴。
具体来说,我使用的是FuncTickFormatter.from_py_func函数。
下面的代码示例不会产生任何结果(但也不会产生错误消息)。
from bokeh.models import ColumnDataSource,Label, FuncTickFormatter,DatetimeTickFormatter,NumeralTickFormatter, Select, FixedTicker, Slider,TableColumn,DatePicker, DataTable, TextInput, HoverTool,Range1d,BoxZoomTool, ResetTool
from bokeh.plotting import figure, output_file, show, curdoc
from bokeh.layouts import row, column, widgetbox, layout
from bokeh.io import output_notebook, push_notebook, show
output_notebook()
x = np.arange(10)
y = [random.uniform(0,5000) for el in x]
xfactors = list("abcdefghi")
yrange = Range1d(0,5000)
p = figure(x_range = xfactors, y_range = yrange,y_minor_ticks = 10)
p.circle(x,y, size = 14, line_color = "grey" , fill_color = "lightblue", fill_alpha = 0.2)
def ticker():
a = '{:0,.0f}'.format(tick).replace(",", "X").replace(".", ",").replace("X", ".")
return a
# If I comment below line out, code is running just fine
p.yaxis.formatter = FuncTickFormatter.from_py_func(ticker)
show(p)如果我注释掉FuncTickFormatter行,代码就会运行得很好。此外,如果我在此代码之外使用定义的函数ticker,它也能工作。
任何关于我做错了什么的建议都会很有帮助。
谢谢!
发布于 2017-02-21 20:51:16
如果from_py_func给您带来麻烦,请使用直接的Javascript。下面是一个例子:
p.yaxis.formatter = FuncTickFormatter(code="""
function(tick){
function markCommas(x) {
return x.toFixed(1).replace(/\B(?=(\d{3})+(?!\d))/g, "X");
}
return markCommas(tick).replace('.',',').replace("X",'.')
}
""")在一些文档中,可能不需要使用tick作为输入参数来定义函数,因此可能需要删除该外部函数,但在我的0.12.2版本中,这可以生成您所要求的数字,例如5.000,0
在较新的版本中,它可能如下所示:
p.yaxis.formatter = FuncTickFormatter(code="""
function markCommas(x) {
return x.toFixed(1).replace(/\B(?=(\d{3})+(?!\d))/g, "X");
}
return markCommas(tick).replace('.',',').replace("X",'.')
""")如果子函数不能工作,下面是一个单行返回语句:
p.yaxis.formatter = FuncTickFormatter(code="""
return tick.toFixed(1).replace(/\B(?=(\d{3})+(?!\d))/g, "X").replace('.',',').replace("X",'.');
""")https://stackoverflow.com/questions/42376878
复制相似问题