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>

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.2.1.tar.gz (17.0 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.2.1-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mpl_typst-0.2.1.tar.gz
Algorithm Hash digest
SHA256 15822e491906e4901ae137f97be90be503d881b2e39798f179cc0bb7fa5f043b
MD5 35bee188dc6f7becbc7634eef906c0b1
BLAKE2b-256 6148582b8c32c05752b229e85f2cd4e0cc51a0542859a42dca155c01a410eb7f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mpl_typst-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5993708c547a02d9fad98914db83e8c33ff57a739de4009b9a9bb2b7e38edbda
MD5 3e5b26cd0c16456a609ccd31b0452190
BLAKE2b-256 200c76171ceddb2b4876390ecdd2111ef329094a47a09a72c2ca6302c5335a40

See more details on using hashes here.

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