从下拉列表更新vbar\U堆栈边界

2024-10-04 01:23:46 发布

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

每次我从下拉列表中选择不同的类别时,我都试图在bokeh中更新vbar\u堆栈图,但是由于legend_label在vbar\u图中,我无法在update函数中更新它

我将添加代码以使其更清楚

def make_stacked_bar():

    colors = ["#A3E4D7", "#1ABC9C", "#117A65", "#5D6D7E", "#2E86C1", "#1E8449", "#A3E4D7", "#1ABC9C", "#117A65",
              "#5D6D7E", "#2E86C1", "#1E8449"]
    industries_ = sorted(np.unique(stb_src.data['industries']))
    p = figure(x_range=industries_, plot_height=800, plot_width=1200, title="Impact range weight by industry")

    targets = list(set(list(stb_src.data.keys())) - set(['industries', 'index']))

    p.vbar_stack(targets, x='industries', width=0.9, legend_label=targets, color=colors[:len(targets)], source=stb_src)

以下是更新函数:

def update(attr, old, new):

    stb_src.data.update(make_dataset_stack().data)
    stb.x_range.factors = sorted(np.unique(stb_src.data['industries']))

如何更新实际数据而不仅仅是x轴? 谢谢


Tags: 函数srcdatamakedefupdaterangelabel
1条回答
网友
1楼 · 发布于 2024-10-04 01:23:46

这需要一些非琐碎的工作才能实现。vbar_stack方法是一个方便的函数,它实际创建了多个glyph渲染器,在初始堆栈中,每个“行”对应一个。更重要的是,渲染器通过Stack变换相互关联,该变换在每个步骤中堆叠所有先前的渲染器。因此,实际上并没有任何简单的方法可以更改事后堆叠的行数。以至于我建议在每个回调中删除并重新创建整个绘图(我通常不推荐这种方法,但这种情况是少数例外。)

下面是一个基于select小部件更新整个绘图的完整示例:

from bokeh.layouts import column
from bokeh.models import Select
from bokeh.plotting import curdoc, figure

select = Select(options=["1", "2", "3", "4"], value="1")

def make_plot():
    p = figure()
    p.circle(x=[0,2], y=[0, 5], size=15)
    p.circle(x=1, y=float(select.value), color="red", size=15)
    return p

layout = column(select, make_plot())

def update(attr, old, new):
    p = make_plot()    # make a new plot
    layout.children[1] = p  # replace the old plot

select.on_change('value', update)

curdoc().add_root(layout)

相关问题 更多 >