简体   繁体   中英

python bokeh plotting package does not cache plots in jupyter notebook

Been running into a frustrating problem with the bokeh plotting package in python. So I have a jupyter notebook (notebook version 5.0.0) in which I have some bokeh plots. The notebook is pretty large now, so it does take a bit of time to load. Anyhow, when I was using Matplotlib the images in the notebook would be cached. That way I would not need to rerun them each time I ran the notebook.

Bokeh has the same ability to cache the images, but I cannot seem to get the image cache to work. So for a very simple example, if I had the following code in a notebook:

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)

Running this plot works just fine. But when I saved the notebook, closed it, and reopened it, the plot would not show up again.

Anyone else have this issue.

I figured out the answer to this question. So the problem was with the Jupyter initialization cells extension. I had the code to do the imports of bokeh in the initialization cells at the bottom of my notebook. But when I did this for some reason the plotting cells that were earlier in the notebook were not displaying the cached images.

So I had to basically put the imports of the initialization cells at the very beginning of the notebook. Only then did the caching of images work. Interesting problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM