python 散景绘图包不会在 jupyter notebook 中缓存绘图

python bokeh plotting package does not cache plots in jupyter notebook

运行 遇到 python 中 bokeh 绘图包的令人沮丧的问题。所以我有一个 jupyter 笔记本(笔记本版本 5.0.0),其中有一些散景图。笔记本现在很大,所以加载确实需要一些时间。无论如何,当我使用 Matplotlib 时,笔记本中的图像将被缓存。这样我就不需要每次 运行 笔记本时都重新运行它们。

Bokeh 具有相同的缓存图像的能力,但我似乎无法让图像缓存工作。所以对于一个非常简单的例子,如果我在笔记本中有以下代码:

from bokeh.resources import INLINE
import builtins
import os, sys
import time
import pyugend
import datetime
from IPython.lib import deepreload
builtins.reload = deepreload.reload
from ipywidgets import widgets
from IPython.display import display
from bokeh.io import show, output_notebook
from bokeh.layouts import gridplot
from bokeh.palettes import Viridis3
from bokeh.plotting import figure
from bokeh.charts import defaults
from bokeh import mpl
defaults.width = 700
defaults.height = 700

output_notebook(resources=INLINE)
#output_notebook()
#notebook_handle=True
%reload_ext autoreload
time.sleep(1)

from bokeh.sampledata.iris import flowers

colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]

p = figure(title = "Iris Morphology")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Petal Width'

p.circle(flowers["petal_length"], flowers["petal_width"],
         color=colors, fill_alpha=0.2, size=10)


show(p)

运行 这个情节很好用。但是当我保存笔记本,关闭它并重新打开它时,情节又不会出现了。

其他人也有这个问题。

我找到了这个问题的答案。所以问题出在 Jupyter 初始化单元格扩展上。我有代码在笔记本底部的初始化单元格中导入 bokeh。但是当我出于某种原因这样做时,笔记本中较早的绘图单元没有显示缓存的图像。

所以我不得不基本上将初始化单元格的导入放在笔记本的最开头。只有这样图像缓存才起作用。有趣的问题。