gpt4 book ai didi

python - Bokeh :使用编辑工具时禁用自动量程

转载 作者:太空宇宙 更新时间:2023-11-04 01:46:33 25 4
gpt4 key购买 nike

我已经包含了 PolyDrawTool在我的 Bokeh 图中让用户圈点。当用户在图的边缘附近画一条线时,该工具会扩展通常会弄乱形状的轴。有没有办法在用户绘制绘图时卡住坐标轴?

我正在使用 Bokeh 1.3.4

MRE:

import numpy as np
import pandas as pd
import string

from bokeh.io import show
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.models import PolyDrawTool, MultiLine


def prepare_plot():
embedding_df = pd.DataFrame(np.random.random((100, 2)), columns=['x', 'y'])
embedding_df['word'] = embedding_df.apply(lambda x: ''.join(np.random.choice(list(string.ascii_lowercase), (8,))), axis=1)

# Plot preparation configuration Data source
source = ColumnDataSource(ColumnDataSource.from_df(embedding_df))
labels = LabelSet(x="x", y="y", text="word", y_offset=-10,x_offset = 5,
text_font_size="10pt", text_color="#555555",
source=source, text_align='center')
plot = figure(plot_width=1000, plot_height=500, active_scroll="wheel_zoom",
tools='pan, box_select, wheel_zoom, save, reset')

# Configure free-hand draw
draw_source = ColumnDataSource(data={'xs': [], 'ys': [], 'color': []})
renderer = plot.multi_line('xs', 'ys', line_width=5, alpha=0.4, color='color', source=draw_source)
renderer.selection_glyph = MultiLine(line_color='color', line_width=5, line_alpha=0.8)
draw_tool = PolyDrawTool(renderers=[renderer], empty_value='red')
plot.add_tools(draw_tool)

# Add the data and labels to plot
plot.circle("x", "y", size=0, source=source, line_color="black", fill_alpha=0.8)
plot.add_layout(labels)
return plot


if __name__ == '__main__':
plot = prepare_plot()
show(plot)

GIF of the issue

最佳答案

PolyDrawTool 实际上更新了一个ColumnDataSource 来驱动一个字形来绘制用户指示的内容。您看到的行为是这一事实的自然结果,结合 Bokeh 的默认自动范围 DataRange1d(默认情况下,在计算自动边界时也会考虑每个字形)。因此,您有两个选择:

  • 根本不要使用 DataRange1d,例如您可以在调用 figure 时提供固定轴边界:

    p = figure(..., x_range=(0,10), y_range=(-20, 20)

    或者您可以在事后设置它们:

    p.x_range = Range1d(0, 10)
    p.y_range = Range1d(-20, 20)

    当然,使用这种方法您将不再获得任何自动量程;您需要将轴范围设置为您想要的精确开始/结束。

  • 通过显式设置 renderers 属性使 DataRange1d 更具选择性:

    r = p.circle(...)
    p.x_range.renderers = [r]
    p.y_range.renderers = [r]

    现在 DataRange 模型在计算自动范围开始/结束时将只考虑圆形渲染器。

关于python - Bokeh :使用编辑工具时禁用自动量程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com