Bokeh布局中如何让多个图表均响应resize大小调整
问题原因
你仅对第一个图表plot做了下拉选择器的联动绑定,第二个图表plot2没有对应的属性关联逻辑,所以不会响应sizing_mode的变更。
解决方法
在原有绑定逻辑的基础上,新增一行针对plot2的js_link绑定即可,修改后完整代码如下:
from bokeh.core.enums import SizingMode from bokeh.layouts import column from bokeh.models import Select from bokeh.plotting import figure, output_file, show from bokeh.sampledata.iris import flowers as df colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'} colors = [colormap[x] for x in df.species] plot = figure(sizing_mode="fixed") plot.circle(df.petal_length, df.petal_width, color=colors, alpha=0.2, size=10) plot2 = figure(sizing_mode="fixed") plot2.circle(df.petal_length, df.petal_width, color=colors, alpha=0.2, size=10) select = Select(title="Sizing mode", value="fixed", options=list(SizingMode), width=300) select.js_link('value', plot, 'sizing_mode') # 新增此行绑定第二个图表的sizing_mode属性 select.js_link('value', plot2, 'sizing_mode') layout = column(select, plot, plot2) layout.sizing_mode = "stretch_both" # set separately to avoid also setting children output_file("sizing_mode.html", title="sizing_mode.py example") show(layout)
后续如果新增更多图表,只需给每个图表都添加对应的js_link绑定即可实现同步响应。
内容的提问来源于stack exchange,提问作者kchsu8
相关产品推荐
相关产品推荐

