简体   繁体   English

Python Bokeh:在缩放时将 X 轴重新启动到 0

[英]Python Bokeh: Restart X axis to 0 on Zoom

I have code below that creates a simple line xy plot.我有下面的代码可以创建一个简单的线 xy 图。

When I zoom in, I want the x-axis ticker to start at 0 again instead of 3.9/whatever the x point of the zoom was as in the image.当我放大时,我希望 x 轴自动收报机再次从 0 开始,而不是 3.9/无论缩放的 x 点在图像中是什么。

No Zoom:无变焦:

在此处输入图片说明

After Zooming:缩放后:

在此处输入图片说明

How do I do that?我怎么做?

Code:代码:

from bokeh.io import output_file, show, save
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource

data = []
x = list(range(11))
y0 = x
y1 = [10 - xx for xx in x]
y2 = [abs(xx - 5) for xx in x]
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1, y2=y2))
for i in range(3):
    p = figure(title="Title " + str(i), plot_width=300, plot_height=300)
    if len(data):
        p.x_range = data[0].x_range
        p.y_range = data[0].y_range

    p.circle('x', 'y0', size=10, color="navy", alpha=0.5, legend_label='line1', source=source)

    p.legend.location = 'top_right'
    p.legend.click_policy = "hide"
    data.append(p)
plot_col = column(data)
# show the results
show(plot_col)

This is an unusual requirement, and none of the built-in things behave this way.这是一个不寻常的要求,没有任何内置的东西是这样的。 If you zoom in to the interval [4,7] , the the range will be updated [4, 7] , and so then the axis will display labels for [4, 7] .如果放大到区间[4,7] ,则范围将更新[4, 7] ,因此轴将显示[4, 7] 的标签。 If it will suffice to simply display different tick labels, even while the underlying range start/end remain their usual values, then you could use a Custom Extension to generate whatever customized labels you want.如果简单地显示不同的刻度标签就足够了,即使基础范围开始/结束仍然是它们的通常值,那么您可以使用自定义扩展来生成您想要的任何自定义标签。 There is an example in the User's Guide that already does almost exactly what you want already:用户指南中有一个示例,它已经几乎完全符合您的要求:

https://docs.bokeh.org/en/latest/docs/user_guide/extensions_gallery/ticking.html#userguide-extensions-examples-ticking https://docs.bokeh.org/en/latest/docs/user_guide/extensions_gallery/ticking.html#userguide-extensions-examples-ticking

You might also be able to do something even more simply with a FuncTickFormatter , eg (untested)您也可以使用FuncTickFormatter更简单地做一些事情,例如(未经测试)

p.xaxis.formatter = FuncTickFormatter(code="""
    return tick - ticks[0]
""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM