Search code examples
bokehholoviews

How to format colorbar label in Bokeh


I have generated a holoviews \ Bokeh heatmap graph similar to the example.
I added a colorbar in the plot_opts with colorbar=True, but the labels of the colorbar comes out as a scientific number.
How do I control the format of the label? I tried implementing the following code without success:

cbf = PrintfTickFormatter(format='0,0')
color_bar = ColorBar(formatter=cbf)

and then add it to the plot_opts but it did not change anything.

I looked at this user guide documentation and this model documentation but did not manage to figure out how to implement the property.

UPDATE
Here is the full code Which still doesn't work.

dfqudf.head()  

|INDEX|dow|hour|QUERYTYPE|
|-------|-------|--------|-------------|
|72| Sunday| 0 |205104|
|24| Monday| 0 |210293|
|120| Tuesday| 0 |206381|
|144| Wednesday| 0 |212874|
|96| Thursday| 0 |216195|

hv.extension('bokeh')
colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
heatmap = hv.HeatMap(data=dfqudf[['hour','dow','QUERYTYPE']]
                     , label="Query Hour by Weekday")

hover = HoverTool(tooltips=[('Num of Queries', '@QUERYTYPE{0,0}')])

formatter = NumeralTickFormatter(format='0,0')  #PrintfTickFormatter(format='%.1f') doesn't work either
color_bar = {'formatter': formatter}
plot_opts = dict(width=900, height=300, xrotation=45, xaxis='top', labelled=[]
                 , tools=[hover], toolbar='below'
                 , colorbar=True, colorbar_opts= color_bar
                )
style = dict(cmap=ListedColormap(colors))

heatmap(plot=plot_opts, style=style)

Solution

  • Using hv.help(element) you can find out what options you can customize an Element with. Elements that support colormapping (including HeatMap) accept a colorbar_opts plot option which are passed through to the ColorBar constructor. Here's a very simple example:

    from bokeh.models import PrintfTickFormatter
    formatter = PrintfTickFormatter(format='%.1f')
    plot_opts = dict(colorbar=True, colorbar_opts={'formatter': formatter})
    hv.HeatMap([(0,0,0), (1,1,1)]).opts(plot=plot_opts)