2
0

More than 1 year has passed since last update.

Bokeh グラフ再利用時のRuntimeError:Models must be ...に対応する

Last updated at Posted at 2022-07-17

Bokehで一度描画したグラフを、違うレイアウトで再利用しようとするとRuntimeError: Models must be owned by only a single document, ... というエラーがでます。

from bokeh.plotting import figure, show
from bokeh.layouts import gridplot

p = figure()
p.scatter([1, 2, 3], [4, 5, 6])

show(p)

show(gridplot([[p]]))  # ここでエラー

これは一度showしたところで、グラフにIDがつくためのようです。
このエラーを回避するためには、IDをリセットする必要があります。

from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import Model

p = figure()
p.scatter([1, 2, 3], [4, 5, 6])

show(p)

# p内部のModel型の要素を選択して、IDをリセットする
for model in p.select(Model):
    prev_doc = model.document
    model._document = None
    if prev_doc:
        prev_doc.remove_root(model)

show(gridplot([[p]]))

日本語ではあまり情報が探せなかったので、備忘録として書いておきました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0