Bokeh RUNTIME错误:模型只能由单个文档所拥有,选择(id=';1057';,...)已在文档中 [英] Bokeh RuntimeError: Models must be owned by only a single document, Selection(id='1057', ...) is already in a doc
本文介绍了Bokeh RUNTIME错误:模型只能由单个文档所拥有,选择(id=';1057';,...)已在文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在Bokeh中绘制多个图表,但出现以下错误:
RuntimeError: Models must be owned by only a single document, Selection(id='1057', ...) is already in a doc.
我知道这是在尝试在多个文档中使用相同对象时导致的,但我不知道我在哪里这样做。以下是完整(简化)代码。
我使用的是Bokeh 1.4.0。
from bokeh.plotting import figure, show
from bokeh.layouts import row, gridplot
from bokeh.models import ColumnDataSource
from bokeh.io import output_file
import pandas as pd
feature_groups = [['ciao'],['bye']]
df = pd.DataFrame.from_dict({'x':[0,1,2,3,4], 'y':[2,3,4,5,6]})
x_test = [0,1,2,3,4]
y_test = [2,3,4,5,6]
source = ColumnDataSource(df)
for features_columns in feature_groups:
output_file('features_labels' + features_columns[0] +'.html')
p = []
for k,f in enumerate(features_columns):
p_k = figure(title=f)
p_k.circle(x=f, y='ki', line_width=2, source=source,fill_alpha=0.5,line_alpha=0.5)
p_k.circle_cross( x=x_test, y=y_test, color='red',fill_alpha=0.5,line_alpha=0.5)
p_k.circle_cross( x = x_test, y = y_test, color='green',fill_alpha=0.5,line_alpha=0.5)
p_k.xaxis.axis_label = f
p_k.yaxis.axis_label = 'ki'
p.append(p_k)
grid = gridplot(p, ncols=2)
show(grid)
提前感谢您
推荐答案
1)就像错误所说的,每个Bokeh模型(在本例中是的实例)只能添加到Bokeh中一次,因此只需将source = ColumnDataSource(df)
移到for
循环中。
Document
中的字形和曲线图之间共享相同的source
,而不能在不同的文档之间共享。像output_file
、save
和show
这样的方法隐式创建一个新的BokehDocument
,因此将相同的source
与原始代码中的两个output_file
语句结合使用总是会导致问题
2)您引用了不存在于ColumnDataSource中的字段,如‘ki’等。我将它们替换为x='x'
和y='y'
请参阅下面已更正且正常工作的代码:
from bokeh.plotting import figure, show
from bokeh.layouts import row, gridplot
from bokeh.models import ColumnDataSource
from bokeh.io import output_file
import pandas as pd
feature_groups = [['ciao'],['bye']]
df = pd.DataFrame.from_dict({'x':[0,1,2,3,4], 'y':[2,3,4,5,6]})
x_test = [0,1,2,3,4]
y_test = [2,3,4,5,6]
for features_columns in feature_groups:
output_file('features_labels' + features_columns[0] +'.html')
p = []
source = ColumnDataSource(df)
for k,f in enumerate(features_columns):
p_k = figure(title=f)
p_k.circle(x='x', y='y', line_width=2, source=source,fill_alpha=0.5,line_alpha=0.5)
p_k.circle_cross( x=x_test, y=y_test, color='red',fill_alpha=0.5,line_alpha=0.5)
p_k.circle_cross( x = x_test, y = y_test, color='green',fill_alpha=0.5,line_alpha=0.5)
p_k.xaxis.axis_label = f
p_k.yaxis.axis_label = 'ki'
p.append(p_k)
grid = gridplot(p, ncols=2)
show(grid)
这篇关于Bokeh RUNTIME错误:模型只能由单个文档所拥有,选择(id=';1057';,...)已在文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文