跳转到主要内容

将Vega-Lite图表规范转换为SVG、PNG或Vega

项目描述

概述

vl-convert-python 是一个无需依赖的Python包,用于将 Vega-Lite 图表规范转换为静态图像(SVG或PNG)或 Vega 图表规范。

由于Altair图表可以生成Vega-Lite,因此可以使用此包轻松地将Altair图表转换为静态图像。

在Binder上试用!
Binder

安装

可以使用pip通过以下方式安装 vl-convert-python

$ pip install vl-convert-python

用法

vl-convert-python 包在 vl_convert 模块下提供一系列转换函数。

将Vega-Lite转换为SVG、PNG和Vega

可以使用 vegalite_to_svgvegalite_to_png 函数分别将Vega-Lite规范转换为静态SVG和PNG图像。可以使用 vegalite_to_vega 函数将Vega-Lite规范转换为Vega规范。

import vl_convert as vlc
import json

vl_spec = r"""
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "https://raw.githubusercontent.com/vega/vega-datasets/next/data/movies.json"},
  "mark": "circle",
  "encoding": {
    "x": {
      "bin": {"maxbins": 10},
      "field": "IMDB Rating"
    },
    "y": {
      "bin": {"maxbins": 10},
      "field": "Rotten Tomatoes Rating"
    },
    "size": {"aggregate": "count"}
  }
}
"""

# Create SVG image string and then write to a file
svg_str = vlc.vegalite_to_svg(vl_spec=vl_spec)
with open("chart.svg", "wt") as f:
    f.write(svg_str)

# Create PNG image data and then write to a file
png_data = vlc.vegalite_to_png(vl_spec=vl_spec, scale=2)
with open("chart.png", "wb") as f:
    f.write(png_data)

# Create low-level Vega representation of chart and write to file
vg_spec = vlc.vegalite_to_vega(vl_spec)
with open("chart.vg.json", "wt") as f:
    json.dump(vg_spec, f)

将Altair图表转换为SVG、PNG和Vega

Altair可视化库提供了一种Pythonic API来生成Vega-Lite可视化。因此,可以使用 vl-convert-python 将Altair图表转换为PNG、SVG或Vega。 vegalite_* 函数支持一个可选的 vl_version 参数,可以用来指定要使用的特定版本的Vega-Lite JavaScript库。Altair包的版本4.2使用Vega-Lite版本4.17,因此在将Altair图表转换为时,应指定此版本。

import altair as alt
from vega_datasets import data
import vl_convert as vlc
import json

source = data.barley()

chart = alt.Chart(source).mark_bar().encode(
    x='sum(yield)',
    y='variety',
    color='site'
)

# Create SVG image string and then write to a file
svg_str = vlc.vegalite_to_svg(chart.to_json(), vl_version="4.17")
with open("altair_chart.svg", "wt") as f:
    f.write(svg_str)

# Create PNG image data and then write to a file
png_data = vlc.vegalite_to_png(chart.to_json(), vl_version="4.17", scale=2)
with open("altair_chart.png", "wb") as f:
    f.write(png_data)

# Create low-level Vega representation of chart and write to file
vg_spec = vlc.vegalite_to_vega(chart.to_json(), vl_version="4.17")
with open("altair_chart.vg.json", "wt") as f:
    json.dump(vg_spec, f)

工作原理

本软件包使用PyO3vl-convert-rs Rust 软件包封装为 Python 库。该 vl-convert-rs 软件包是一个独立的 Rust 库,用于将Vega-Lite 可视化规范转换为各种格式。转换操作使用在由 deno_runtime 软件包提供的 v8 JavaScript 运行时中运行的 Vega-Lite 和 Vega JavaScript 库执行。字体指标和 SVG-to-PNG 转换由 resvg 软件包提供。

值得注意的是,vl-convert-python 完全独立,不依赖于外部网页浏览器或 Node.js 运行时。

开发设置

创建开发 conda 环境

$ conda create -n vl-convert-dev -c conda-forge python=3.10 deno maturin altair pytest black black-jupyter scikit-image

激活环境并使用 pip 安装剩余的依赖项

$ conda activate vl-convert-dev
$ pip install pypdfium2

切换到 Python 包目录

$ cd vl-convert-python

使用 maturin 在开发模式下构建 Rust Python 包

$ maturin develop --release

运行测试

$ pytest tests

项目详情


下载文件

根据您的平台下载文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分布

vl_convert_python-1.6.1.tar.gz (4.7 MB 查看哈希值)

上传时间 源代码

构建分布

vl_convert_python-1.6.1-cp37-abi3-win_amd64.whl (28.6 MB 查看哈希值)

上传时间 CPython 3.7+ Windows x86-64

vl_convert_python-1.6.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (29.4 MB 查看哈希值)

上传时间 CPython 3.7+ manylinux: glibc 2.17+ x86-64

vl_convert_python-1.6.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (28.5 MB 查看哈希值)

上传于 CPython 3.7+ manylinux: glibc 2.17+ ARM64

vl_convert_python-1.6.1-cp37-abi3-macosx_11_0_arm64.whl (26.3 MB 查看哈希值)

上传于 CPython 3.7+ macOS 11.0+ ARM64

vl_convert_python-1.6.1-cp37-abi3-macosx_10_12_x86_64.whl (27.5 MB 查看哈希值)

上传于 CPython 3.7+ macOS 10.12+ x86-64

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面