簡體   English   中英

Python Bokeh:在縮放時將 X 軸重新啟動到 0

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

我有下面的代碼可以創建一個簡單的線 xy 圖。

當我放大時,我希望 x 軸自動收報機再次從 0 開始,而不是 3.9/無論縮放的 x 點在圖像中是什么。

無變焦:

在此處輸入圖片說明

縮放后:

在此處輸入圖片說明

我怎么做?

代碼:

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)

這是一個不尋常的要求,沒有任何內置的東西是這樣的。 如果放大到區間[4,7] ,則范圍將更新[4, 7] ,因此軸將顯示[4, 7] 的標簽。 如果簡單地顯示不同的刻度標簽就足夠了,即使基礎范圍開始/結束仍然是它們的通常值,那么您可以使用自定義擴展來生成您想要的任何自定義標簽。 用戶指南中有一個示例,它已經幾乎完全符合您的要求:

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

您也可以使用FuncTickFormatter更簡單地做一些事情,例如(未經測試)

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

暫無
暫無

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

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