簡體   English   中英

顯示散景標簽值精確到小數點后兩位(浮點精度誤差)

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

我正在使用 Bokeh 創建一個熱圖,數據源是 Pandas 數據框( monthly_rets )。 標簽被填充值從一個稱為列returns在DF,這是float數據類型的。 一些標簽顯示有非常多的小數位 (0.130000000007)。
如何將熱圖單元格標簽中顯示的值限制為僅顯示 2 個小數位?
我目前的代碼如下:

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)

我不確定,但試試round(source,2) 或者,如果你只是想切斷其余的source = int(source * 100) / 100

事實證明 Bokeh 需要顯示一個字符串,並且所有字符串操作都需要在將其傳遞給 Bokeh 數據源之前進行。 要解決此問題,您需要按如下方式強制 Pandas 列:

monthly_rets['returns'] = monthly_rets['returns'].astype(str)
monthly_rets['returns'] = monthly_rets['returns'].str.slice(0,5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM