繁体   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