从下拉列表中vbar_stack散景更新



每次从下拉列表中选择不同的类别时,我都会尝试在散景中更新vbar_stack图,但由于legend_label在vbar_plot内,因此无法在更新功能中更新它。

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

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 轴? 谢谢!

这将需要一些不平凡的工作来实现。vbar_stack方法是一个方便的函数,它实际上创建了多个字形呈现器初始堆叠中的每个"行"对应一个字形呈现器。更重要的是,渲染器都是相互关联的,通过Stack转换,在每一步堆叠所有以前的渲染器。因此,实际上没有任何简单的方法来更改事后堆叠的行数。如此之多,以至于我建议在每个回调中简单地删除并重新创建整个情节。(我通常不推荐这种方法,但这种情况是为数不多的例外之一。

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

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)

最新更新