Python / 散景 - FuncTickFormatter
Python / Bokeh - FuncTickFormatter
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
.
我正在使用 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
也可以工作。
任何关于我做错了什么的建议都会很有帮助。
谢谢!
如果 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",'.');
""")
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 usingfrom_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
也可以工作。
任何关于我做错了什么的建议都会很有帮助。
谢谢!
如果 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",'.');
""")