python - VSCode 笔记本静默显示失败(modify_doc)
问题描述
在 Visual Studio Code 中使用内置的 Jupyter Notebook 编辑器并使用show(modify_doc)
绘图的方式时,不显示结果。显示单个图确实有效。
我尝试使用谷歌搜索并阅读文档,但在任何地方都找不到解决方案。希望有人知道这里出了什么问题。
(示例取自网络)
导入必要的模块:
from bokeh.io import push_notebook, show, output_notebook
from bokeh.layouts import row, gridplot
from bokeh.plotting import figure, show, output_file, curdoc
from bokeh.document import Document
output_notebook()
import numpy as np
这在 VSCode 中不能按预期工作,但在官方 Jupyter Notebook 中可以。我添加了一个打印语句作为测试,它也没有在 VSCode 中打印。
def modify_doc(doc):
print("Test")
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"
p1 = figure(title="Legend Example", tools=TOOLS)
p1.circle(x, y, legend="sin(x)")
p1.circle(x, 2*y, legend="2*sin(x)", color="orange")
p1.circle(x, 3*y, legend="3*sin(x)", color="green")
# Add everything to the layout
layout = row(p1)
# Add the layout to curdoc
doc.add_root(layout)
show(modify_doc)
以下代码确实运行:
x2 = np.linspace(0, 4*np.pi, 100)
y2 = np.sin(x2)
TOOLS2 = "pan,wheel_zoom,box_zoom,reset,save,box_select"
p2 = figure(title="Legend Example", tools=TOOLS2)
p2.circle(x2, y2, legend="sin(x)")
p2.circle(x2, 2*y2, legend="2*sin(x)", color="orange")
p2.circle(x2, 3*y2, legend="3*sin(x)", color="green")
show(p2)