[英]Bokeh how to show negative log scale in x and y axis?
我想 plot 一些范圍很廣的財務數據。 起初我使用線性軸,但是由於 x 和 y 軸的極端范圍...... plot 最終無法使用。 我知道有異常值,但我不想將它們從圖表中排除。
因此,我對 x 和 y 軸都使用對數刻度。 已成功創建對數刻度 plot,但它只顯示正數據...所有負數據都從 plot 中消失。 然后我做了一些搜索,我在 Bokeh github 中發現了關於強制日志軸保持正數: https://github.com/bokeh/bokeh/issues/5550
使用來自 github 的信息,真的不可能創建由負值組成的對數刻度嗎? 我想要的是圖表能夠將 x 和 y 軸都擴展到負值並能夠顯示完整數據(因此無需排除異常值)。
這是我寫的代碼:
p = figure(
x_axis_type = 'log',
y_axis_type = 'log',
height = 600,
sizing_mode = "stretch_width",
tools = TOOLS,
tooltips = TOOLTIPS,
toolbar_location = "above"
)
p.xaxis.major_label_orientation = 3.14 / 4
p.xaxis[0].formatter = NumeralTickFormatter(format="0")
p.yaxis[0].formatter = NumeralTickFormatter(format="0")
p.grid.grid_line_alpha = 0.3
low_x = fundamental['Profit [%]'].min()
high_x = fundamental['Profit [%]'].max()
low_y = fundamental['Profit Growth [%]'].min()
high_y = fundamental['Profit Growth [%]'].max()
p.line(x = (low_x, 0), y = (0, 0), color = 'red', line_width = 2, line_dash = 'dashed')
p.line(x = (0, 0), y = (low_y, 0), color = 'red', line_width = 2, line_dash = 'dashed')
p.line(x = (0, high_x), y = (0, 0), color = 'green', line_width = 2, line_dash = 'dashed')
p.line(x = (0, 0), y = (0, high_y), color = 'green', line_width = 2, line_dash = 'dashed')
show(p)
只要數據包含負值,數據就可以只是隨機浮點數。 我在 Windows 10 機器上使用 Python 3.8.13 和 Bokeh 2.4.3。 干杯!
在數學上,對log
function 在零處未定義(“無限”),並且對於負實數是復數值。 所以在純粹意義上,負對數軸是不可能的。 一些庫(例如我認為的 MPL)已經實現了一個symlog
(“對稱日志”)軸選項,該選項在零附近的某個間隔內線性化比例,並使用負值的幅度,並將范圍縫合在一起,以便“明確定義” . 但是,這種方法更適合不支持平移和縮放的 static 圖。 將它添加到 Bokeh 將是一筆不小的返工,而且對它的需求從來沒有太多,所以沒有人決定花時間在它上面。
其他一些選項:
DataTable
旁邊的數據表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.