Bokeh 简明教程

Bokeh - Exporting Plots

除上面所述的子命令外,可以使用 export() 函数以 PNG 和 SVG 文件格式导出 Bokeh 图表。为此,本地 Python 安装应具备以下依赖库。

In addition to subcommands described above, Bokeh plots can be exported to PNG and SVG file format using export() function. For that purpose, local Python installation should have following dependency libraries.

PhantomJS

PhantomJS 是一个 JavaScript API,它支持自动导航、屏幕截图、用户行为和断言。它用于运行基于浏览器的单元测试。PhantomJS 基于 WebKit,为不同浏览器提供类似的浏览环境,并为各种 Web 标准提供快速且本地的支持:DOM 处理、CSS 选择器、JSON、画布和 SVG。换句话说,PhantomJS 是一款没有图形用户界面的 Web 浏览器。

PhantomJS is a JavaScript API that enables automated navigation, screenshots, user behavior and assertions. It is used to run browser-based unit tests. PhantomJS is based on WebKit providing a similar browsing environment for different browsers and provides fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. In other words, PhantomJS is a web browser without a graphical user interface.

Pillow

Pillow,一个 Python 成像库(早前称为 PIL)是一个免费的 Python 编程语言库,它支持打开、操作和保存许多不同的图像文件格式。(包括 PPM、PNG、JPEG、GIF、TIFF 和 BMP。)它的部分功能包括逐像素操作、遮罩和透明度处理、图像滤镜、图像增强等。

Pillow, a Python Imaging Library (earlier known as PIL) is a free library for the Python programming language that provides support for opening, manipulating, and saving many different image file formats. (including PPM, PNG, JPEG, GIF, TIFF, and BMP.) Some of its features are per-pixel manipulations, masking and transparency handling, image filtering, image enhancing, etc.

export_png() 函数从布局生成 RGBA 格式的 PNG 图像。此函数使用无头 Webkit 浏览器在内存中呈现布局,然后捕获屏幕截图。生成的图像将与源布局具有相同的尺寸。确保 Plot.background_fill_color 和 Plot.border_fill_color 属性为 None。

The export_png() function generates RGBA-format PNG image from layout. This function uses Webkit headless browser to render the layout in memory and then capture a screenshot. The generated image will be of the same dimensions as the source layout. Make sure that the Plot.background_fill_color and Plot.border_fill_color are properties to None.

from bokeh.io import export_png
export_png(plot, filename = "file.png")

可以使用诸如 Adobe Illustrator 等程序编辑带有 SVG 元素的 HTML5 画布图表输出。SVG 对象也可以转换为 PDF。在此,canvas2svg 是一个 JavaScript 库,它用于使用 SVG 元素模拟普通的画布元素及其方法。与 PNG 相同,为了使用透明背景创建 SVG,Plot.background_fill_color 和 Plot.border_fill_color 属性应为 None。

It is possible that HTML5 Canvas plot output with a SVG element that can be edited using programs such as Adobe Illustrator. The SVG objects can also be converted to PDFs. Here, canvas2svg, a JavaScript library is used to mock the normal Canvas element and its methods with an SVG element. Like PNGs, in order to create a SVG with a transparent background,the Plot.background_fill_color and Plot.border_fill_color properties should be to None.

首先,通过将 Plot.output_backend 属性设为“svg”来激活 SVG 后端。

The SVG backend is first activated by setting the Plot.output_backend attribute to "svg".

plot.output_backend = "svg"

对于无头导出,Bokeh 有一个实用函数 export_svgs()。此函数会将布局中所有支持 SVG 的图表下载为不同的 SVG 文件。

For headless export, Bokeh has a utility function, export_svgs(). This function will download all of SVG-enabled plots within a layout as distinct SVG files.

from bokeh.io import export_svgs
plot.output_backend = "svg"
export_svgs(plot, filename = "plot.svg")