2017-02-21 53 views
2

EDIT: thanks to @tmwilson26 I was able to fix it using javascript code (see comments below). However, I would still be interested to know if there is a solution using from_py_func .Python /散焦 - FuncTickFormatter

我正在使用散景,并努力使用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,也可以使用。

任何关于我做错的建议都会非常有帮助。

谢谢!

+0

现在'tick'在'ticker'的内部范围或主外部范围内的程序中没有被定义。 “tick”应该是“ticker”的输入参数吗? – tmwilson26

+0

是的,我知道。但是我理解文档的方式我不必定义它(参见[Bokeh](http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#functickformatter))。显然, t虽然... – FredMaster

+0

也许我的版本不喜欢它,因为它返回'ValueError:Function“func”只能有一个参数,但是0提供.'如果我没有定义'tick'作为输入,也就是说,除非我使用'%'而不是'格式'方法重新格式化字符串,否则这个图仍​​然是空白的。 – tmwilson26

回答

1

如果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",'.') 
""") 

如果子功能不起作用,这里是一个单行return语句:

p.yaxis.formatter = FuncTickFormatter(code=""" 
    return tick.toFixed(1).replace(/\B(?=(\d{3})+(?!\d))/g, "X").replace('.',',').replace("X",'.'); 
""") 
+0

这是在你的系统上工作。对我来说,它不工作 - 既不在'jupyter'也不使用'散景服务'。和以前一样:没有错误信息,没有阴谋...... – FredMaster

+0

是的。我有一个旧版本的散景(虽然最新版本是0.12.4,我有0.12.2),我希望这不会是一个问题,但可惜。尝试删除外部功能。我会用格式更新我的答案,以便您可以看到,但是我无法在我的版本上进行测试,因此请告诉我该方案是否有效。 – tmwilson26

+0

如果这不起作用,我怀疑这是因为子功能定义,如果需要可以删除。 – tmwilson26