Plotly 简明教程
Plotly - Exporting to Static Images
离线图形的输出可以导出为各种光栅和矢量图像格式。为此,我们需要安装两个依赖关系 – orca 和 psutil 。
Outputs of offline graphs can be exported to various raster and vector image formats. For that purpose, we need to install two dependencies – orca and psutil.
Orca
Orca 代表 Open-source Report Creator App 。它是一个 Electron 应用,用于从命令行生成 plotly 图形、控制面板应用和控制面板的图像和报告。Orca 是 Plotly 的 Image Server 的支柱。
Orca stands for Open-source Report Creator App. It is an Electron app that generates images and reports of plotly graphs, dash apps, dashboards from the command line. Orca is the backbone of Plotly’s Image Server.
psutil
psutil (python system and process utilities) 是一个跨平台库,用于在 Python 中检索正在运行的进程和系统利用率的信息。它实现了 UNIX 命令行工具提供的许多功能,如: ps, top, netstat, ifconfig, who, 等。psutil 支持所有主要的运营系统,如 Linux、Windows 和 MacOs
psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization in Python. It implements many functionalities offered by UNIX command line tools such as: ps, top, netstat, ifconfig, who, etc. psutil supports all major operating systems such as Linux, Windows and MacOs
Installation of Orca and psutil
如果您使用的是 Python 的 Anaconda 发行版,通过 conda package manager 可以非常容易地安装 orca 和 psutil,如下所示 −
If you are using Anaconda distribution of Python, installation of orca and psutil is very easily done by conda package manager as follows −
conda install -c plotly plotly-orca psutil
因为 orca 在 PyPi 存储库中不可用。您可以使用 npm utility 来替代安装它。
Since, orca is not available in PyPi repository. You can instead use npm utility to install it.
npm install -g electron@1.8.4 orca
使用 pip 安装 psutil
Use pip to install psutil
pip install psutil
如果你无法使用 npm 或 conda,也可以从以下可通过 https://github.com/plotly/orca/releases. 获得的网站上下载 orca 的预构建二进制文件
If you are not able to use npm or conda, prebuilt binaries of orca can also be downloaded from the following website which is available at https://github.com/plotly/orca/releases.
要以 png、jpg 或 WebP 格式导出 Figure 对象,首先导入 plotly.io 模块
To export Figure object to png, jpg or WebP format, first, import plotly.io module
import plotly.io as pio
现在,我们可以按如下所示调用 write_image() 函数 −
Now, we can call write_image() function as follows −
pio.write_image(fig, ‘sinewave.png’)
pio.write_image(fig, ‘sinewave.jpeg’)
pio.write_image(fig,’sinewave.webp)
orca 工具还支持将 plotly 导出为 svg、pdf 和 eps 格式。
The orca tool also supports exporting plotly to svg, pdf and eps formats.
Pio.write_image(fig, ‘sinewave.svg’)
pio.write_image(fig, ‘sinewave.pdf’)
在 Jupyter notebook 中,通过 pio.to_image() 函数获得的图像对象可以按如下所示内联显示 −
In Jupyter notebook, the image object obtained by pio.to_image() function can be displayed inline as follows −