[英]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.