How to change a format string after using a formatter

I tried to call a formatter on my yaxis of a plot and then I wanted to extend this with a postfix.
I did mention this on github first, but I was not able to produce a valid result from the answer.
This is my example.

import pandas as pd

from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource, NumeralTickFormatter, PrintfTickFormatter
output_notebook()

df = pd.DataFrame({'BitsPerSeconde': [26666, 13333, 40000, 13333, 16666]})
p = figure()

p.line(x='index', y='BitsPerSeconde', color='blue', source=df)
p.yaxis.formatter =  NumeralTickFormatter(format="0.0 b", name="foo")
# {"@{foo}-some-postfix"} 
p.yaxis.axis_label = 'bits per second'
show(p)

My goal is to have labels on the y-axix like this: “2 KiBpS”.

@mosc9575

One approach would be to use the FuncTickFormatter, as shown in the following modification to your example.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"""
import pandas as pd

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

df = pd.DataFrame({'BitsPerSeconde': [26666, 13333, 40000, 13333, 16666]})
p = figure()

p.line(x='index', y='BitsPerSeconde', color='blue', source=df)
p.yaxis.formatter = FuncTickFormatter(code='''return (tick/1000.0) + ' ' + 'KiBpS';''')
#p.yaxis.formatter =  NumeralTickFormatter(format="0.0 b", name="foo")
# {"@{foo}-some-postfix"} 
p.yaxis.axis_label = 'bits per second'
show(p)
1 Like