Search code examples
python-3.xbokehholoviewsbokehjs

Use € as currency symbol in NumeralTickFormatter from bokeh


I would like to use the € symbol instead of $ to format my numbers in a bokeh plot that is created by holoviews (hv.Bars).

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

Unfortunately, this does only yield a formatted number but not the Euro symbol

Also, the work-around as mentioned here

How to format bokeh xaxis ticks with currency

with

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

does not work.

I actually think that bokeh should adapt to this and provide the possibility to add anything as symbol.


Solution

  • This can be done using FuncTickFormatter and a bit of TypeScript code.

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

    Minimal Example If your goal is to edit the x-axis for values between 0 and 1e7 this should work. This will select no unit for values smaller than 1000, k for values between 1000 and 1e6, and m for bigger values.

    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)
    

    Output

    using FuncTickFormatter