☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

Display Bokeh labels values correct to 2 decimal places (floating point precision error)

Community Support
avagut
avagut

I am creating a heatmap using Bokeh with the datasource being a Pandas dataframe ( monthly_rets ). The labels are being populated with values from a column called returns in the df, which is of float datatype. Some of the labels are showing up with very many decimal places (0.130000000007).
How can I limit the values being displayed in the heatmap cells labels to show only 2 decimal places?
My current code is as below:

source = ColumnDataSource(monthly_rets)
plot = figure(title="Monthly Returns (%)",
                x_range=months, y_range=years,
                plot_height=300, plot_width=800)
plot.rect(x="rtn_month", y="rtn_year", width=1, height=1,
            source=monthly_rets,
            fill_color={'field': 'returns', 'transform': mapper},
            name="returns")
labels = LabelSet(x="rtn_month", y="rtn_year", text="returns",
                    text_font_size="10pt", source=source, 
                   text_align='center')
plot.add_layout(labels)

Any help appreciated.

⌁   Link to post
Bryan
Bryan

What is actually in the "returns" column of the data source? It seems like the values are already text, which means they are formatted and an earlier place than this, and you should fix the problem there, using standard Python numeric formatting techniques.

⌁   Link to post
avagut
avagut

Hi Bryan,
Thank you for replying.
The returns columns is a pandas non-null float64 column as picked from df.info() on the dataframe used by source. I tried first converting the dataframe columns to strings passing it to source but it threw this error: ValueError: expected a value of type Real, got -0.04 of type str

⌁   Link to post
avagut
avagut

@Bryan,
Sorry, I am a first user on Discourse.
Below, I shall show what I have tried.
I have successfully recreated the error in a Jupyter notebook although I was originally running it in a Flask app.

#Setup bokeh in a jupyter notebook

import pandas as pd
from bokeh.plotting import figure
from bokeh.util.string import encode_utf8
from bokeh.models import Title, LinearAxis
from bokeh.models import (LinearColorMapper, BasicTicker, PrintfTickFormatter,
                          ColorBar, Label, LabelSet, ColumnDataSource)
from bokeh.models.formatters import DatetimeTickFormatter
from bokeh.palettes import RdYlGn
from bokeh.io import output_notebook, show
output_notebook()

#Load the data from json file

orig_data = '{"columns":["rtn_month","log_ret","returns","rtn_year"],"index":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35],"data":[["Apr",0.001,0.08,"2010 -"],["Aug",0.0,0.04,"2010 -"],["Dec",-0.004,-0.36,"2010 -"],["Feb",0.004,0.406,"2010 -"],["Jan",-0.001,-0.054,"2010 -"],["Jul",0.0,0.0,"2010 -"],["Jun",0.0,0.0,"2010 -"],["Mar",0.007,0.688,"2010 -"],["May",0.004,0.442,"2010 -"],["Nov",-0.004,-0.399,"2010 -"],["Oct",-0.002,-0.239,"2010 -"],["Sep",0.004,0.44,"2010 -"],["Apr",-0.001,-0.081,"2011 -"],["Aug",-0.004,-0.404,"2011 -"],["Dec",-0.007,-0.655,"2011 -"],["Feb",-0.002,-0.2,"2011 -"],["Jan",0.006,0.563,"2011 -"],["Jul",-0.002,-0.161,"2011 -"],["Jun",0.004,0.364,"2011 -"],["Mar",-0.01,-1.041,"2011 -"],["May",0.001,0.081,"2011 -"],["Nov",-0.004,-0.367,"2011 -"],["Oct",0.0,0.0,"2011 -"],["Sep",-0.006,-0.608,"2011 -"],["Apr",-0.001,-0.082,"2012 -"],["Aug",-0.005,-0.484,"2012 -"],["Dec",0.004,0.363,"2012 -"],["Feb",0.004,0.41,"2012 -"],["Jan",0.004,0.412,"2012 -"],["Jul",0.006,0.608,"2012 -"],["Jun",0.008,0.776,"2012 -"],["Mar",0.002,0.204,"2012 -"],["May",-0.001,-0.082,"2012 -"],["Nov",0.0,-0.04,"2012 -"],["Oct",0.002,0.243,"2012 -"],["Sep",0.001,0.121,"2012 -"]]}'

monthly_rets = pd.read_json(orig_data, orient='split')
print(monthly_rets)  # This results in nicely formatted data in the dataframe

#Create bokeh plot with label showing very many decimal places
years = (list(monthly_rets.rtn_year.unique()))
years.reverse()
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
low = monthly_rets.returns.min()
high = monthly_rets.returns.max()
source = ColumnDataSource(monthly_rets)

TOOLS = "hover,save,pan,box_zoom,reset,wheel_zoom"
raw_colors = RdYlGn[11].copy()
colors = raw_colors
colors.reverse()
mapper = LinearColorMapper(palette=colors, low=low, high=high)
plot = figure(title="Monthly returns (%)",
              x_range=months, y_range=years,
              x_axis_location="below",
              tools=TOOLS,
              toolbar_location='below',
              plot_height=300, plot_width=800)
plot.grid.grid_line_color = None
plot.axis.axis_line_color = None
plot.axis.major_tick_line_color = None
plot.axis.major_label_text_font_size = "10pt"
plot.axis.major_label_standoff = 0
plot.rect(x="rtn_month", y="rtn_year", width=1, height=1,
          source=monthly_rets,
          fill_color={'field': 'returns', 'transform': mapper},
          line_color=None,
          name="returns")
labels = LabelSet(x="rtn_month", y="rtn_year", text="returns", y_offset=0,
                  text_font_size="10pt", text_color="black",
                  source=source, text_align='center')
plot.add_layout(labels)
color_bar = ColorBar(color_mapper=mapper,
                     major_label_text_font_size="10pt",
                     ticker=BasicTicker(desired_num_ticks=len(RdYlGn[11])),
                     formatter=PrintfTickFormatter(format="%d%%"),
                     label_standoff=10, border_line_color=None,
                     location=(0, 0))
plot.add_layout(color_bar, 'right')
show(plot)

Please advise if there is any other information that would be required on this

⌁   Link to post

End of archived discussion · Replies are disabled