散景x_axis_type持续时间?

时间:2016-12-12 16:40:05

标签: python bokeh

我的散景图上的x轴表示five seconds的持续时间,而不是像2016-01-01 12:00:00这样的时间。有没有办法在我的Bokeh x轴上适当地渲染刻度线?设置x_axis_type='datetime'并没有做正确的事情,从下面的图中重复0ms可以看出:

enter image description here

1 个答案:

答案 0 :(得分:1)

On Bokeh 0.12.6,you can use PrintfTickFormatter

from bokeh.plotting import figure, output_file, show
from bokeh.models import PrintfTickFormatter

output_file('output.html')

p = figure(plot_width=400, plot_height=400) p.line(x, y, size=1)

# must be applied to the 1st element, not the axis itself 
p.xaxis[0].formatter = PrintfTickFormatter(format="%sms")

show(p)

你甚至不必设置x_axis_type='datetime',它甚至可以用线性轴工作。

编辑:要应用单位的自定义格式,例如ms / s / min,you have to use FuncTickFormatter,因为它太复杂,目前Bokeh无法处理。从0.12.6开始,有两种方法可以使用它。

首先,通过使用转换器将Python函数转换为Javascript代码,通过Flexxpip install flexx)。它将所有内容保留在Python语法之下,但需要额外的依赖性。

from bokeh.plotting import figure, output_file, show
from bokeh.models import FuncTickFormatter

output_file('output.html')

p = figure(plot_width=400, plot_height=400) p.line(x, y, size=1)

# custom formatter function
def custom_formatter():
    units = [
        ('min', 60000.0),
        ('s', 1000.0),
        ('ms', 1.0),
    ]
    for u in units:
        if tick >= u[1]:
            return '{}{}'.format(tick / u[1], u[0])


# must be applied to the 1st element, not the axis itself 
p.xaxis[0].formatter = FuncTickFormatter.from_py_func(custom_formatter)

show(p)

最后,将实际的Javascript代码编写为字符串并将其作为参数传递给formatter。 Bokeh本地化。请记住,您无法控制客户端环境,因此请避免使用纯vanilla Javascript以外的任何其他内容。

from bokeh.plotting import figure, output_file, show
from bokeh.models import FuncTickFormatter

output_file('output.html')

p = figure(plot_width=400, plot_height=400) p.line(x, y, size=1)

units = [
        ('min', 60000.0),
        ('s', 1000.0),
        ('ms', 1.0),
    ]

# must be applied to the 1st element, not the axis itself 
p.xaxis[0].formatter = FuncTickFormatter(code=""" var units = {'min':
60000.0, 's': 1000.0, 'ms': 1.0}; for (u in units) {
    if (tick >= units[u]) {
        return (tick / units[u] + u);
    } } """)

show(p)

我发现它有点烦人,但那是我如何为我的应用程序修复轴。我发现需要对名为tick的变量进行硬编码,这是一种糟糕的编程习惯。希望Bokeh能在不久的将来提供更好的解决方案。