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.1.tar.gz (22.4 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.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mpl_typst-0.3.1.tar.gz
  • Upload date:
  • Size: 22.4 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.1.tar.gz
Algorithm Hash digest
SHA256 1a1476dcc6be6f242abdecc633687cc09b3e12579caad6058484e5dcbe28486f
MD5 e18381a14213b18fb63547e41c22ffea
BLAKE2b-256 62be8f6ffaf84c6eed6ab51456565a6720998bc80e2dffe261a820f7ae917bbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for mpl_typst-0.3.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: mpl_typst-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 23.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b7c735d89967a897dd8efaa8978183b7425523c4a79e1fc66a9a67850d13509
MD5 56af7532390bb234a9a398c905f5489d
BLAKE2b-256 23e1a694d18b31d2e44fa71a6ad01bfecee35f97bd65321bc00704cca5f8bc7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mpl_typst-0.3.1-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