umbruch-matplotlib 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- umbruch_matplotlib-0.1.0/LICENSE +21 -0
- umbruch_matplotlib-0.1.0/PKG-INFO +98 -0
- umbruch_matplotlib-0.1.0/README.md +73 -0
- umbruch_matplotlib-0.1.0/pyproject.toml +34 -0
- umbruch_matplotlib-0.1.0/setup.cfg +4 -0
- umbruch_matplotlib-0.1.0/tests/test_umbruch_matplotlib.py +756 -0
- umbruch_matplotlib-0.1.0/umbruch_matplotlib.egg-info/PKG-INFO +98 -0
- umbruch_matplotlib-0.1.0/umbruch_matplotlib.egg-info/SOURCES.txt +10 -0
- umbruch_matplotlib-0.1.0/umbruch_matplotlib.egg-info/dependency_links.txt +1 -0
- umbruch_matplotlib-0.1.0/umbruch_matplotlib.egg-info/requires.txt +5 -0
- umbruch_matplotlib-0.1.0/umbruch_matplotlib.egg-info/top_level.txt +1 -0
- umbruch_matplotlib-0.1.0/umbruch_matplotlib.py +1085 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Manuel Reinhardt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: umbruch-matplotlib
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Matplotlib backend that renders PDF, text and math through umbruch
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/manuel-rhdt/umbruch-matplotlib
|
|
7
|
+
Project-URL: Repository, https://github.com/manuel-rhdt/umbruch-matplotlib
|
|
8
|
+
Project-URL: Issues, https://github.com/manuel-rhdt/umbruch-matplotlib/issues
|
|
9
|
+
Keywords: matplotlib,backend,pdf,png,typography,mathml
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Printing
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: matplotlib>=3.9
|
|
21
|
+
Requires-Dist: umbruch>=0.8.0
|
|
22
|
+
Provides-Extra: math
|
|
23
|
+
Requires-Dist: latex2mathml>=3.77; extra == "math"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# umbruch-matplotlib
|
|
27
|
+
|
|
28
|
+
A Matplotlib backend that renders plots to PDF or PNG using with
|
|
29
|
+
[umbruch](https://github.com/manuel-rhdt/umbruch) as the backend. Text and math are laid out by umbruch:
|
|
30
|
+
labels may contain rich text (`rich("<i>t</i><sub>0</sub>")`) and `$...$` math is converted to
|
|
31
|
+
MathML. It talks to umbruch through the `umbruch` package (`umbruch.Server`, version 0.8.0 or later); the executable comes with the package, and `$UMBRUCH` or `PATH` override it.
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import matplotlib
|
|
35
|
+
matplotlib.use("module://umbruch_matplotlib")
|
|
36
|
+
import matplotlib.pyplot as plt
|
|
37
|
+
from umbruch_matplotlib import rich
|
|
38
|
+
|
|
39
|
+
fig, ax = plt.subplots()
|
|
40
|
+
ax.set_xlabel(rich("time <i>t</i> (ms)"))
|
|
41
|
+
ax.set_title(r"$\hat\rho_{ij}=\frac{a}{b}$")
|
|
42
|
+
fig.savefig("fig.pdf")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Install with `pip install .[math]` (the `math` extra pulls in `latex2mathml`).
|
|
46
|
+
|
|
47
|
+
`savefig("fig.png")` is rasterized by umbruch as well: the same laid-out page as the PDF, painted
|
|
48
|
+
at the figure's `dpi` (96 dpi is only umbruch's default, Matplotlib's is used), so the image has the
|
|
49
|
+
same layout, the same rich text and the same math as the PDF and nothing falls back to Matplotlib's
|
|
50
|
+
Agg. The page background is the figure's facecolor, so a transparent figure comes out with an alpha
|
|
51
|
+
channel. One page per file — a figure is one page — and a `dpi` that needs more than 16.7 million
|
|
52
|
+
pixels in total (the raster backend's limit) is an error.
|
|
53
|
+
|
|
54
|
+
The backend builds on parts of Matplotlib's SVG renderer that are not public API. It is tested with Matplotlib 3.9, 3.10 and 3.11 (CI runs the oldest and the newest), and on import it checks that the parts it needs are there, naming what is missing otherwise.
|
|
55
|
+
|
|
56
|
+
Unsupported features raise instead of falling back to Matplotlib's own text rendering:
|
|
57
|
+
`text.usetex`, and TeX that cannot be converted to MathML.
|
|
58
|
+
|
|
59
|
+
Fonts come from Matplotlib: for every text, the font file that `font_manager.findfont` picks (including fonts added with `fontManager.addfont`) is sent to umbruch as an `@font-face` with a `data:` URI, so the PDF uses the face Matplotlib measured, with real italic and bold faces, and nothing depends on umbruch seeing the system's fonts. Styled markup inside a `rich()` string (`<b>`, `<strong>`, `<i>`, `<em>`) is set in the bold and italic faces of the *same* family: when a text has any, the family's bold, italic and bold-italic faces are sent along and all of them carry one `@font-face` family name, which is what CSS font matching needs (it never leaves the first family). The families of the text follow as CSS fallbacks. Measuring sends each face once, as the style sheet that umbruch remembers (`Server.set_stylesheet`, which needs umbruch 0.5.3). The SVG that is rendered to PDF still carries its own `@font-face` rules, because it is the same SVG that `savefig("x.svg")` writes. Variable fonts are drawn in their default instance, and from a `.ttc` only the first face is read.
|
|
60
|
+
|
|
61
|
+
Tick labels with exponents (`10^3`, `1×10^-4`) are set as text with superscripts in the label font, not as math. Math in `$...$` is converted to MathML and set in STIX Two Math by default. `mathtext.fontset = "cm"` selects Latin Modern Math, and `umbruch_matplotlib.config.math_font` takes the family of any OpenType math font that Matplotlib finds (for example `"Libertinus Math"`, sent to umbruch like the text fonts) or the path of a font file; it needs a font with an OpenType MATH table. Gouraud triangles (`pcolormesh`/`tripcolor` with `shading="gouraud"`) are written as one compact `<g data-umbruch-mesh="gouraud">` element with shared vertices that umbruch paints as a single native shading (an umbruch extension of SVG that nothing else draws; it needs a umbruch that reports `mesh_gouraud`). `config.gouraud = "subdivide"` draws them as flat triangles and `"stock"` uses Matplotlib's gradient emulation, for other renderers, and `"auto"` picks the native mesh when it is possible. A native mesh needs one alpha for the whole mesh (otherwise it raises), and no Gouraud mode works in a wide colour space yet, see below.
|
|
62
|
+
|
|
63
|
+
### Wide-gamut colours
|
|
64
|
+
|
|
65
|
+
Matplotlib has no colour management: every RGB value (colours, colormaps, image pixels) is three numbers in [0, 1]. Say which space they are in, and umbruch writes the PDF in that space:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
umbruch_matplotlib.config.color_space = "display-p3" # or fig.savefig(..., color_space="display-p3")
|
|
69
|
+
ax.plot(x, y, color=(0.1, 0.9, 0.3)) # these numbers are Display P3
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The spaces are those of CSS Color 4: `srgb` (the default), `display-p3`, `a98-rgb`, `prophoto-rgb` and `rec2020`. Painted colours (fill, stroke, text, hatches) are written as `color(display-p3 r g b)` from Matplotlib's float values, without the rounding to 1/255 of stock SVG; no hex colour is left in the SVG. Raster images are 8 bits per channel as Matplotlib makes them. (Matplotlib's own gradient emulation for Gouraud triangles, `config.gouraud = "stock"`, is gone: it only wrote hex colours.)
|
|
73
|
+
|
|
74
|
+
Everything Matplotlib itself supplies as sRGB (names like `"red"`, hex colours, the default colour cycle, the built-in colormaps) is then misread as P3 numbers. Convert it: `convert_color("tab:blue", dst="display-p3")` and `convert_cmap("viridis", dst="display-p3")` return values in the target space, clipped to its gamut.
|
|
75
|
+
|
|
76
|
+
Raster images (`imshow`, rasterised artists) get an ICC profile of the space in their PNG, which umbruch uses as the colour space of the image. Gouraud shading (`shading="gouraud"`) raises `NotImplementedError` in a wide space for now, because umbruch paints meshes in sRGB; use `shading="flat"` or `"nearest"`.
|
|
77
|
+
|
|
78
|
+
## Examples
|
|
79
|
+
|
|
80
|
+
Rich text through `rich()`: inline markup for italics, bold, sub- and superscripts, colour and size, in titles, labels, tick labels and annotations.
|
|
81
|
+
|
|
82
|
+

|
|
83
|
+
|
|
84
|
+
Math is laid out as MathML with STIX Two Math, with correct operator spacing and function application, and can be mixed with rich text.
|
|
85
|
+
|
|
86
|
+

|
|
87
|
+
|
|
88
|
+
A larger figure with hatching, gradients, colour bars, annotations, and math and rich text in titles, labels and legends. The output is a vector PDF whose text stays selectable.
|
|
89
|
+
|
|
90
|
+

|
|
91
|
+
|
|
92
|
+
The scripts are in `examples/`. `python examples/make_images.py` re-renders the PNGs (needs `pdftoppm` and an `umbruch` binary on `$UMBRUCH` or the path).
|
|
93
|
+
|
|
94
|
+
Tests: `pytest tests`.
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT, see `LICENSE`.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# umbruch-matplotlib
|
|
2
|
+
|
|
3
|
+
A Matplotlib backend that renders plots to PDF or PNG using with
|
|
4
|
+
[umbruch](https://github.com/manuel-rhdt/umbruch) as the backend. Text and math are laid out by umbruch:
|
|
5
|
+
labels may contain rich text (`rich("<i>t</i><sub>0</sub>")`) and `$...$` math is converted to
|
|
6
|
+
MathML. It talks to umbruch through the `umbruch` package (`umbruch.Server`, version 0.8.0 or later); the executable comes with the package, and `$UMBRUCH` or `PATH` override it.
|
|
7
|
+
|
|
8
|
+
```python
|
|
9
|
+
import matplotlib
|
|
10
|
+
matplotlib.use("module://umbruch_matplotlib")
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
from umbruch_matplotlib import rich
|
|
13
|
+
|
|
14
|
+
fig, ax = plt.subplots()
|
|
15
|
+
ax.set_xlabel(rich("time <i>t</i> (ms)"))
|
|
16
|
+
ax.set_title(r"$\hat\rho_{ij}=\frac{a}{b}$")
|
|
17
|
+
fig.savefig("fig.pdf")
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Install with `pip install .[math]` (the `math` extra pulls in `latex2mathml`).
|
|
21
|
+
|
|
22
|
+
`savefig("fig.png")` is rasterized by umbruch as well: the same laid-out page as the PDF, painted
|
|
23
|
+
at the figure's `dpi` (96 dpi is only umbruch's default, Matplotlib's is used), so the image has the
|
|
24
|
+
same layout, the same rich text and the same math as the PDF and nothing falls back to Matplotlib's
|
|
25
|
+
Agg. The page background is the figure's facecolor, so a transparent figure comes out with an alpha
|
|
26
|
+
channel. One page per file — a figure is one page — and a `dpi` that needs more than 16.7 million
|
|
27
|
+
pixels in total (the raster backend's limit) is an error.
|
|
28
|
+
|
|
29
|
+
The backend builds on parts of Matplotlib's SVG renderer that are not public API. It is tested with Matplotlib 3.9, 3.10 and 3.11 (CI runs the oldest and the newest), and on import it checks that the parts it needs are there, naming what is missing otherwise.
|
|
30
|
+
|
|
31
|
+
Unsupported features raise instead of falling back to Matplotlib's own text rendering:
|
|
32
|
+
`text.usetex`, and TeX that cannot be converted to MathML.
|
|
33
|
+
|
|
34
|
+
Fonts come from Matplotlib: for every text, the font file that `font_manager.findfont` picks (including fonts added with `fontManager.addfont`) is sent to umbruch as an `@font-face` with a `data:` URI, so the PDF uses the face Matplotlib measured, with real italic and bold faces, and nothing depends on umbruch seeing the system's fonts. Styled markup inside a `rich()` string (`<b>`, `<strong>`, `<i>`, `<em>`) is set in the bold and italic faces of the *same* family: when a text has any, the family's bold, italic and bold-italic faces are sent along and all of them carry one `@font-face` family name, which is what CSS font matching needs (it never leaves the first family). The families of the text follow as CSS fallbacks. Measuring sends each face once, as the style sheet that umbruch remembers (`Server.set_stylesheet`, which needs umbruch 0.5.3). The SVG that is rendered to PDF still carries its own `@font-face` rules, because it is the same SVG that `savefig("x.svg")` writes. Variable fonts are drawn in their default instance, and from a `.ttc` only the first face is read.
|
|
35
|
+
|
|
36
|
+
Tick labels with exponents (`10^3`, `1×10^-4`) are set as text with superscripts in the label font, not as math. Math in `$...$` is converted to MathML and set in STIX Two Math by default. `mathtext.fontset = "cm"` selects Latin Modern Math, and `umbruch_matplotlib.config.math_font` takes the family of any OpenType math font that Matplotlib finds (for example `"Libertinus Math"`, sent to umbruch like the text fonts) or the path of a font file; it needs a font with an OpenType MATH table. Gouraud triangles (`pcolormesh`/`tripcolor` with `shading="gouraud"`) are written as one compact `<g data-umbruch-mesh="gouraud">` element with shared vertices that umbruch paints as a single native shading (an umbruch extension of SVG that nothing else draws; it needs a umbruch that reports `mesh_gouraud`). `config.gouraud = "subdivide"` draws them as flat triangles and `"stock"` uses Matplotlib's gradient emulation, for other renderers, and `"auto"` picks the native mesh when it is possible. A native mesh needs one alpha for the whole mesh (otherwise it raises), and no Gouraud mode works in a wide colour space yet, see below.
|
|
37
|
+
|
|
38
|
+
### Wide-gamut colours
|
|
39
|
+
|
|
40
|
+
Matplotlib has no colour management: every RGB value (colours, colormaps, image pixels) is three numbers in [0, 1]. Say which space they are in, and umbruch writes the PDF in that space:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
umbruch_matplotlib.config.color_space = "display-p3" # or fig.savefig(..., color_space="display-p3")
|
|
44
|
+
ax.plot(x, y, color=(0.1, 0.9, 0.3)) # these numbers are Display P3
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The spaces are those of CSS Color 4: `srgb` (the default), `display-p3`, `a98-rgb`, `prophoto-rgb` and `rec2020`. Painted colours (fill, stroke, text, hatches) are written as `color(display-p3 r g b)` from Matplotlib's float values, without the rounding to 1/255 of stock SVG; no hex colour is left in the SVG. Raster images are 8 bits per channel as Matplotlib makes them. (Matplotlib's own gradient emulation for Gouraud triangles, `config.gouraud = "stock"`, is gone: it only wrote hex colours.)
|
|
48
|
+
|
|
49
|
+
Everything Matplotlib itself supplies as sRGB (names like `"red"`, hex colours, the default colour cycle, the built-in colormaps) is then misread as P3 numbers. Convert it: `convert_color("tab:blue", dst="display-p3")` and `convert_cmap("viridis", dst="display-p3")` return values in the target space, clipped to its gamut.
|
|
50
|
+
|
|
51
|
+
Raster images (`imshow`, rasterised artists) get an ICC profile of the space in their PNG, which umbruch uses as the colour space of the image. Gouraud shading (`shading="gouraud"`) raises `NotImplementedError` in a wide space for now, because umbruch paints meshes in sRGB; use `shading="flat"` or `"nearest"`.
|
|
52
|
+
|
|
53
|
+
## Examples
|
|
54
|
+
|
|
55
|
+
Rich text through `rich()`: inline markup for italics, bold, sub- and superscripts, colour and size, in titles, labels, tick labels and annotations.
|
|
56
|
+
|
|
57
|
+

|
|
58
|
+
|
|
59
|
+
Math is laid out as MathML with STIX Two Math, with correct operator spacing and function application, and can be mixed with rich text.
|
|
60
|
+
|
|
61
|
+

|
|
62
|
+
|
|
63
|
+
A larger figure with hatching, gradients, colour bars, annotations, and math and rich text in titles, labels and legends. The output is a vector PDF whose text stays selectable.
|
|
64
|
+
|
|
65
|
+

|
|
66
|
+
|
|
67
|
+
The scripts are in `examples/`. `python examples/make_images.py` re-renders the PNGs (needs `pdftoppm` and an `umbruch` binary on `$UMBRUCH` or the path).
|
|
68
|
+
|
|
69
|
+
Tests: `pytest tests`.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
MIT, see `LICENSE`.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "umbruch-matplotlib"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Matplotlib backend that renders PDF, text and math through umbruch"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.12"
|
|
13
|
+
dependencies = ["matplotlib>=3.9", "umbruch>=0.8.0"]
|
|
14
|
+
keywords = ["matplotlib", "backend", "pdf", "png", "typography", "mathml"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Topic :: Printing",
|
|
22
|
+
"Topic :: Scientific/Engineering :: Visualization",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
math = ["latex2mathml>=3.77"]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/manuel-rhdt/umbruch-matplotlib"
|
|
30
|
+
Repository = "https://github.com/manuel-rhdt/umbruch-matplotlib"
|
|
31
|
+
Issues = "https://github.com/manuel-rhdt/umbruch-matplotlib/issues"
|
|
32
|
+
|
|
33
|
+
[tool.setuptools]
|
|
34
|
+
py-modules = ["umbruch_matplotlib"]
|