2017-02-21 55 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