1

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.

KaRaOkEy
  • 318
  • 1
  • 10
  • Bokeh just passes the format string you give to a `PrintfTickFormatter` to the `sprintf-js` library, and that is not a valid format string for that formatter. – bigreddot Feb 03 '21 at 22:36
  • 1
    In any case, if you think that something should be part of the library, the appropriate step to take is to [make a GitHub issue](https://github.com/bokeh/bokeh/issues) that describes your need in detail. – bigreddot Feb 03 '21 at 23:08

2 Answers2

3

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.

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)

Output

using FuncTickFormatter

mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • I appreciate your answer. However, this code is not able to automatically determine if the number should be displayed in 1k / 1m / .. (The intention behind the 'a' format ) – KaRaOkEy Feb 03 '21 at 09:37
  • Well, you adapt the typescript code and making some if cases. – mosc9575 Feb 03 '21 at 09:48
  • Well, I upvote since it does what I asked for but is not a clean solution since my numbers can get really big and I don't appreciate x-elif cases. Thank you anyway :) – KaRaOkEy Feb 03 '21 at 09:59
  • 1
    I edited my solution. I think it is not to hard to make some changes for your case. – mosc9575 Feb 03 '21 at 10:02
2

NumeralTickFormatter and PrintfTickFormatter are different, and utilize entirely different formatting strings. If you want to use PrintfTickFormatter, you need to give it a valid "printf" format string:

PrintfTickFormatter(format='€ %0.2f')

enter image description here

The valid printf formats are all described in the documentation

bigreddot
  • 33,642
  • 5
  • 69
  • 122