保存html文件与散景图像

Lot*_*tte 3 plot image bokeh

我正在创建一个包含多个图像的散景图.我创建并显示我的文件,如下所示:

output_file(my_dir + "Graphs\\graph")
show(bar)
Run Code Online (Sandbox Code Playgroud)

然后它会向我显示该图并在我的目录"Graphs"中创建一个graph.html文件.但是当我稍后打开html时,情节不包含图像.如何保存html文件,以便它还包含图像?

fil*_*ton 12

正如文档中提到的,您有两种方法可以实现此目的:

  • 使用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)
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用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)
    
    Run Code Online (Sandbox Code Playgroud)


Gui*_*ido 10

如果您遇到问题(尤其是在离线环境中工作),您可能需要考虑添加 mode='inline' 参数:

output_file('plot.html', mode='inline')
Run Code Online (Sandbox Code Playgroud)

这可确保所需的 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)
Run Code Online (Sandbox Code Playgroud)

查看此答案以获取进一步参考。