谷歌Colab中的Bokeh

10

Matplotlib无法给我想要的可视化效果。

我喜欢Bokeh的交互功能,我想知道是否有人能够在Google Colab中运行它?

我安装了库(从笔记本本身安装),并显示安装成功。

!pip install bokeh

但是当我使用它时,它没有显示任何东西(甚至没有错误)。只有空白输出。当我检查Chrome的Javascript控制台时,我看到以下信息

Bokeh: 错误:无法运行BokehJS代码,因为缺少BokehJS库


如果有更高声望的人可以添加新标签“google-colab”,那就更好了。 - Abdelwahed
3个回答

26

图表示例笔记本中有一个Bokeh示例。

我怀疑你需要添加的重要部分是:

from bokeh.io import output_notebook
output_notebook()

最重要的是,output_notebook() 必须在同一个单元格内调用。


这适用于预安装的 Bokeh 版本,截至今天为止为 1.4.0。如果我们将其升级到最新的 2.x 版本,则不会显示任何图表。 - Nitin
在普通笔记本电脑上,这没有问题。但在Colab上,会出现一个大红色块,上面写着“Bokeh错误TypeError: Cannot read properties of null (reading 'textContent') at embed_document...." - sh37211

1
我制作了一个库,使在Colab中使用bokeh更加容易。
首先安装它。
!pip install kora

然后您可以轻松绘制图形。
from kora.bokeh import figure
p = figure(100, 200)  # h, w
p.line([1, 2, 3, 4], [6, 7, 2, 4])
p # display itself, don't need show()

如果你调用from kora import bokeh,它可以像import bokeh一样工作。或者你也可以同时使用它们。我的做法只是创建_repr_html_()以帮助显示Figure对象。

0

对于版本在2.x以上的bokeh,想要在Google Colab中显示bokeh图形,请尝试以下操作:

尝试

# Our main plotting package (must have explicit import of submodules)
import bokeh.io
import bokeh.plotting

# Enable viewing Bokeh plots in the notebook
bokeh.io.output_notebook()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接