python-3.x 使用€作为货币符号在NumeralTickFormatter从散景

mdfafbf1  于 4个月前  发布在  Python
关注(0)|答案(2)|浏览(114)

我想使用€符号而不是$来格式化我的数字在一个散景图,是由全息视图(hv.酒吧)创建。

formatter = NumeralTickFormatter(format=f"{€ 0.00 a)")

字符串
不幸的是,这只产生一个格式化的数字,而不是欧元符号
此外,这里提到的变通方法
How to format bokeh xaxis ticks with currency

formatter = PrintfTickFormatter(format=f'€ 0.00 a')


不起作用
我实际上认为散景应该适应这一点,并提供添加任何符号的可能性。

k5hmc34c

k5hmc34c1#

这可以使用FuncTickFormatter和一些TypeScript代码来完成。

from bokeh.models import FuncTickFormatter
p.xaxis.formatter = FuncTickFormatter(code='''Edit some typescript here.''')

字符串

最小示例如果您的目标是编辑0到1e7之间的值的x轴,这应该可以工作。这将为小于1000的值选择无单位,为1000到1e6之间的值选择k,为更大的值选择m

p = figure(width=400, height=400, title=None, toolbar_location="below")
x = [xx*1e6 for xx in range(1,6)]
y = [2, 5, 8, 2, 7]
p.circle(x, y, size=10)

js = """
if (tick < 1e3){
    var unit = ""
    var num =  (tick).toFixed(2)
}
else if (tick < 1e6){
    var unit = "k"
    var num =  (tick/1e3).toFixed(2)
}
else{
    var unit = "m"
    var num =  (tick/1e6).toFixed(2)
}
return `€ ${num} ${unit}`
"""

p.xaxis.formatter = FuncTickFormatter(code=js)
show(p)

输出


的数据

wb1gzix0

wb1gzix02#

NumeralTickFormatterPrintfTickFormatter是不同的,使用完全不同的格式字符串。如果你想使用PrintfTickFormatter,你需要给予一个有效的“printf”格式字符串:

PrintfTickFormatter(format='€ %0.2f')

字符串


的数据
有效的printf格式都在文档中描述

相关问题