Skip to main content

Typst backend for matplotlib (Python visualization library).

Project description

Linting and testing Nightly

Typst Matplotlib Backend

Typst backend for matplotlib (Python visualization library).

Overview

At the moment, Typst supports main vector and raster image formats. Namely, images in PNG, JPEG, GIF, or SVG format can be easily emplaced in a document with Typst. However, it is not possible to keep metadata and annotations. These are mandatory in order to allow a reader to select and interact with vector content (e.g. text) on images. Although SVG can contain text metadata in principle, Typst does not support this feature at the moment but still it is able to render SVG as a vector content.

This package solves this problem for matplotlib users. Basically, this project implements a custom render (or backend) for matplotlib which generates typ-file containing Typst markup. Generated markup file can be later included in the original markup so that the resulting PDF will have interactable content. Matplotlib exploits exactly the same strategy in order to generate PGF-files — a LaTeX markup itself — which can be included into LaTeX markup directly.

Usage

To export a figure using typst, just add

import matplotlib
import mpl_typst

# your drawing code

fig.savefig('figure.typ')

You may call plt.show() afterwards, which displays the figure using your default agg backend.

In order to set the mpl_typst module as default backend (which deactivates interactivity, but also renders SVG, PNG and PDF using typst) one can import mpl_typst.as_default module in order to use mpl_typst backend by default.

import mpl_typst.as_default

Or one can configure it manually.

import matplotlib as mpl
import mpl_typst
mpl.use('module://mpl_typst')

Also, it is possible to use rendering context as usual to override backend.

import matplotlib as mpl
import mpl_typst
with mpl.rc_context({'backend': 'module://mpl_typst'}):  # or mpl_typst.BACKEND
    ...

Next, you can save your figure to typ as usual.

fig, ax = plt.subplots()
...
fig.savefig('line-plot-simple.typ')

As soon as you get a typ-file you can included it directly to figure function and adjust figure time.

#figure(
  include "line-plot-simple.typ",
  kind: image,
  caption: [Simple line plot],
  placement: top,
) <line-plot-simple>

Development Notes

Generated Typst drawing elements use Matplotlib coordinates that have already been normalized to the page's top-left origin. For this reason, every generated Typst place() call must be explicitly anchored with top + left. Do not rely on Typst's default placement alignment; it depends on surrounding document context and can shift clipped drawing elements when a generated figure is included with figure(kind: image).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpl_typst-0.3.0.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mpl_typst-0.3.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file mpl_typst-0.3.0.tar.gz.

File metadata

  • Download URL: mpl_typst-0.3.0.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mpl_typst-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9d6e98985334de596780798dfbb8c6876a45533d7e733fe8792f02fc406f38d3
MD5 ea4417fa87cccc402d44b4bc466083cf
BLAKE2b-256 0e8307d2722fb1b16b4b82eb1d57c80a5bad97a73f50feb0593e67a37ef31a09

See more details on using hashes here.

Provenance

The following attestation bundles were made for mpl_typst-0.3.0.tar.gz:

Publisher: on-release.yml on daskol/mpl-typst

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mpl_typst-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mpl_typst-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mpl_typst-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae43f77293617b1ac14f64110d5bb7f1c635d40c0a5017d9654363ad0c5465a0
MD5 31ddf003add412709eb1efa0c9e0c968
BLAKE2b-256 4475442a48b016edaadd365d725f011957775a9224e4e07c479658f9c3dc1009

See more details on using hashes here.

Provenance

The following attestation bundles were made for mpl_typst-0.3.0-py3-none-any.whl:

Publisher: on-release.yml on daskol/mpl-typst

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page