如何在Python中使用Bokeh BoxEditTool绘制不同颜色的方框?

2024-04-20 05:12:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要实现什么:

我想用BoxEditTool来画画 一个figure中的多个扇区(矩形图示符)。每个绘制扇区(rect) 应该有不同的颜色。颜色必须取自COLORS列表。 此外,绘制的第一个扇区(rect)的颜色应与 COLORS列表中的第一个元素。其他行业也是如此 (第二个扇区颜色->;COLORS[1]

但是,当使用我在这里提供的最小示例时,两个扇区的颜色始终为红色(请参阅提供的图像)。 如果我有第二个渲染器(变量r2),这并不重要,它只使用第一个渲染器(也在BoxEdit工具的doc字符串中描述:”,在绘制新框时,数据将始终添加 到第一个提供的渲染器上的ColumnDataSource。 因此r2似乎毫无用处

我还尝试将'color'属性添加到ColumnDataSource中,但没有成功 的BoxEditTool不允许值(颜色)列表。因此,对我来说,不可能有不同颜色的框

我如何解决这个问题

最小示例:

from bokeh.plotting import figure, output_file, show
from bokeh.io import curdoc
from bokeh.layouts import column, row, layout
from bokeh.models import ColumnDataSource
from bokeh.models.tools import BoxEditTool


COLORS = ["red","blue"]

output_file("box_edit.html")

p = figure(plot_width=400, plot_height=400)

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
))

p.circle('x', 'y', size=20, source=source)

sectordata1 = ColumnDataSource(
    data={'x': [], 'y': [], 'width': [], 'height': []})
sectordata2 = ColumnDataSource(
    data={'x': [], 'y': [], 'width': [], 'height': []})

r1 = p.rect('x', 'y', 'width', 'height', color=COLORS[0], source=sectordata1)
r2 = p.rect('x', 'y', 'width', 'height', color=COLORS[1], source=sectordata2)
tool = BoxEditTool(renderers=[r1, r2],num_objects=2)
p.add_tools(tool)
show(p)

enter image description here


Tags: fromrectimportsource颜色bokeh绘制width