簡體   English   中英

散景如何在 x 和 y 軸上顯示負對數刻度?

[英]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 將是一筆不小的返工,而且對它的需求從來沒有太多,所以沒有人決定花時間在它上面。

其他一些選項:

  • 如果沒有零值,則 plot 使用坐標的絕對值將所有內容保持在正象限中。 然后你可以使用對數刻度。 您可以通過顏色或標記形狀來區分“翻轉”值。
  • 或者,plot 網格 plot 中四個圖中的單獨象限,再次使用絕對值來保持每個 plot 中的值為正,以便可以進行對數日志縮放。 如果需要,您可以酌情翻轉軸。
  • 最后,使用線性比例,但忽略異常值。 找到一些其他方法來顯示異常值,例如在DataTable旁邊的數據表中。

暫無
暫無

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

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