首页 > 解决方案 > 使用 € 作为散景中 NumeralTickFormatter 中的货币符号

问题描述

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

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

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

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

如何用货币格式化散景 xaxis 刻度

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

不起作用。

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

标签: python-3.xbokehholoviewsbokehjs

解决方案


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

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

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

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import FuncTickFormatter
output_notebook()

# create a new plot with the toolbar below
p = figure(plot_width=400, plot_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)
p.xaxis.formatter = FuncTickFormatter(code='''
                                            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}`
                                           '''
                                           )

show(p)

输出

使用 FuncTickFormatter


推荐阅读