在 bokeh 的 NumeralTickFormatter 中使用 € 作为货币符号

问题描述 投票:0回答:2

我想使用 € 符号而不是 $ 来格式化由 Holoviews (hv.Bars) 创建的散景图中的数字。

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

不幸的是,这只会产生一个格式化的数字,但不会产生欧元符号

此外,这里提到的解决方法

如何使用货币格式化散景x轴刻度

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

不起作用。

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

python-3.x bokeh holoviews bokehjs
2个回答
4
投票

这可以使用

FuncTickFormatter
和一些 TypeScript 代码来完成。

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

最小示例 如果您的目标是编辑 x 轴的 0 到 1e7 之间的值,那么这应该可行。这将不会为小于 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)

输出


2
投票

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

PrintfTickFormatter(format='€ %0.2f')

有效的 printf 格式均在文档中描述

© www.soinside.com 2019 - 2024. All rights reserved.