我没有找到有关如何将Bokeh绘图嵌入Markdown样式文档的任何文档。使用bokeh.embed.components中的script和div标签是否有简单的方法可以实现?
<embed>
标签,那么可以使用Bokeh的output_file()
生成的.html
文件来实现这一点。from bokeh.plotting import figure, output_file, show
p = figure(title="example", x_axis_label='x', y_axis_label='y')
output_file("example.html")
show(p) # Needed to actually save the file
在Markdown中:
<embed type="text/html" src="relative/path/to/example.html" width="600" height="400"></embed>
width
和height
属性是必需的,因为它不会自动调整大小。到目前为止,我发现唯一的方法是将图形渲染为图像,然后将其嵌入到Markdown中。
以下是一种方法:
首先将绘图导出为.png文件。因此,您需要安装:
conda install selenium phantomjs pillow
然后你可以使用:
from bokeh.plotting import figure
from bokeh.io import export_png
x = [1, 2, 3, 4, 5]
y = [6, 7, 3, 4, 5]
p = figure(title="example", x_axis_label='x', y_axis_label='y')
p.circle(x, y, legend="circles", line_width=2)
export_png(p, filename="bokeh_plot.png")
![bokeh plot](bokeh_plot.png)
ipython nbconvert --to markdown
,但是图表被破坏了。 - Sean