我正在创建一个包含多个图像的bokeh绘图。我按照以下方式创建并显示文件:
output_file(my_dir + "Graphs\\graph")
show(bar)
然后它向我展示了绘图并在我的“Graphs”目录中创建了一个graph.html文件。但是当我稍后打开html文件时,图表中不包含图片。 我该如何保存html文件,以便它也包含图片?
正如文档中所述,你有两种方法来实现这个:
使用save()
代替show()
from bokeh.plotting import figure, output_file, save
p = figure(title="Basic Title", plot_width=300, plot_height=300)
p.circle([1, 2], [3, 4])
output_file("test.html")
save(p)
使用低级别的file_html
函数
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import file_html
plot = figure()
plot.circle([1,2], [3,4])
html = file_html(plot, CDN, "my plot")
with open("/myPath.html") as f:
f.write(html)
如果遇到问题(特别是在离线环境下工作时),您可能需要考虑添加mode='inline'参数:
output_file('plot.html', mode='inline')
这样可以确保所需的js/css被包含在您的输出html中。这样,您就可以创建一个独立的html文件。
结合现有代码,这将导致:
from bokeh.plotting import figure, output_file, save
p = figure(title="Basic Title", plot_width=300, plot_height=300)
p.circle([1, 2], [3, 4])
output_file('plot.html', mode='inline')
save(p)
请参考此答案以获取更多信息。