A (new) cairo backend for Matplotlib.
Project description
This is a new, essentially complete implementation of a cairo backend for Matplotlib. It can be used in combination with a Qt5, GTK3, Tk, wx, or macOS UI, or non-interactively (i.e., to save figure to various file formats).
Noteworthy points include:
Improved accuracy (e.g., with marker positioning, quad meshes, and text kerning; floating point surfaces are supported with cairo≥1.17.2).
Support for a wider variety of font formats, such as otf and pfb, for vector (PDF, PS, SVG) backends (Matplotlib’s Agg backend also supports such fonts).
Optional support for complex text layout (right-to-left languages, etc.) using Raqm. Note that Raqm depends on Fribidi, which is licensed under the LGPLv2.1+.
Support for embedding URLs in PDF (but not SVG) output (requires cairo≥1.15.4).
Support for multi-page output both for PDF and PS (Matplotlib only supports multi-page PDF).
Support for custom blend modes (see examples/operators.py).
Installation
mplcairo requires
Python≥3.5 (≥3.6 on Windows),
Matplotlib≥2.2 (declared as install_requires),
pybind11≥2.2.4 [1] (declared as install_requires),
on Linux and macOS, pycairo≥1.16.0 [2] (declared as conditional install_requires),
on Windows, cairo≥1.11.4 [3] (shipped with the wheel).
As usual, install using pip:
$ pip install mplcairo # from PyPI
$ pip install git+https://github.com/matplotlib/mplcairo # from Github
Note that wheels are not available for macOS, because no macOS version ships a recent-enough libc++ by default and vendoring of libc++ appears to be fragile. Help for packaging would be welcome.
mplcairo can use Raqm (≥0.2) for complex text layout if it is available. Refer to the instructions on that project’s website for installation on Linux and macOS. You may want to look at https://github.com/HOST-Oman/libraqm-cmake for Windows build scripts.
pycairo 1.16.0 added get_include().
We do not actually rely on pycairo’s Python bindings. Rather, specifying a dependency on pycairo is a convenient way to specify a dependency on cairo (≥1.13.1, for pycairo≥1.14.0) itself, and allows us to load cairo at runtime instead of linking to it (simplifying the build of self-contained wheels).
On Windows, this strategy is (AFAIK) not possible, so we explicitly link against the cairo DLL.
cairo 1.11.4 added mesh gradient support (used by draw_quad_mesh()).
cairo 1.15.4 added support for PDF metadata and links; the presence of this feature is detected at runtime.
cairo 1.17.2 added support for floating point surfaces, usable with mplcairo.set_options(float_surface=True); the presence of this feature is detected at runtime.
On Fedora, the package is available as python-mplcairo.
Building/packaging
This section is only relevant if you wish to build mplcairo yourself, or package it for redistribution. Otherwise, proceed to the Use section.
In all cases, once the dependencies described below are installed, mplcairo can be built and installed using any of the standard commands (pip wheel --no-deps ., pip install ., pip install -e . and python setup.py build_ext -i being the most relevant ones).
Unix
The following additional dependencies are required:
a C++ compiler with C++17 support, e.g. GCC≥7.2 or Clang≥5.0.
cairo and FreeType headers, and pkg-config information to locate them.
If using conda, they can be installed using
conda install -y -c conda-forge pycairo pkg-config
as pycairo (also a dependency) depends on cairo, which depends on freetype. Note that cairo and pkg-config from the anaconda channel will not work.
On Linux, they can also be installed with your distribution’s package manager (Arch: cairo, Debian/Ubuntu: libcairo2-dev, Fedora: cairo-devel).
Raqm (≥0.2) headers are also needed, but will be automatically downloaded if not found.
Linux
conda’s compilers (gxx_linux-64 on the anaconda channel) currently interact poorly with installing cairo and pkg-config from conda-forge, so you are on your own to install a recent compiler (e.g., using your distribution’s package manager). You may want to set the CC and CXX environment variables to point to your C++ compiler if it is nonstandard [4]. In that case, be careful to set them to e.g. g++-7 and not gcc-7, otherwise the compilation will succeed but the shared object will be mis-linked and fail to load.
The manylinux wheel is built using tools/build-manylinux-wheel.sh.
Packagers may want to set the MPLCAIRO_BUILD_TYPE environment variable to package before the build (see the docstring of setup.py for details).
NOTE: On Arch Linux, the python-pillow (Arch) package includes an invalid version of raqm.h (https://bugs.archlinux.org/task/57492) and must not be installed while building mplcairo with the system Python, even in a virtualenv (it can be installed when using mplcairo without causing any problems). One solution is to temporarily uninstall the package; another one is to package it yourself using e.g. pypi2pkgbuild.
distutils uses CC for compiling C++ sources but CXX for linking them (don’t ask). You may run into additional issues if CC or CXX has multiple words; e.g., if CC is set to ccache g++, you also need to set CXX to ccache gcc.
macOS
Clang≥5.0 can be installed from conda’s anaconda channel (conda install -c anaconda clangxx_osx-64), or can also be installed with Homebrew (brew install llvm). Note that Homebrew’s llvm formula is keg-only, i.e. it requires manual modifications to the PATH and LDFLAGS (as documented by brew info llvm).
On macOS<10.14, it is additionally necessary to use clang<8.0 (e.g. with brew install llvm@7) as clang 8.0 appears to believe that code relying on C++17 can only be run on macOS≥10.14+.
The macOS wheel is built using tools/build-macos-wheel.sh, which relies on delocate-wheel (to vendor a recent version of libc++). Currently, it can only be built from a Homebrew-clang wheel, not a conda-clang wheel (due to some path intricacies…).
As I can personally only test the macOS build on CI, any help with the build and the packaging on that platform would be welcome.
Windows
The following additional dependencies are required:
VS2019 (The exact minimum version is unknown, but it is known that mplcairo fails to build on the Azure vs2017-win2016 agent and requires the windows-2019 agent. This is the reason for restricting support to Python 3.6 on Windows: distutils is able to use such a recent MSVC only since Python 3.6.4.)
cairo headers and import and dynamic libraries (cairo.lib and cairo.dll) with FreeType support. Note that this excludes, in particular, the Anaconda and conda-forge builds: they do not include FreeType support.
I am in fact not aware of any such build available online, with the exception of https://github.com/preshing/cairo-windows/releases; however, this specific build appears to misrender pdfs. Instead, a solution is to get the headers e.g. from a Linux distribution package, the DLL from Christoph Gohlke’s cairocffi build, and generate the import library oneself using dumpbin and lib.
FreeType headers and import and dynamic libraries (freetype.lib and freetype.dll), which can be retrieved from https://github.com/ubawurinna/freetype-windows-binaries, or alternatively using conda:
conda install -y freetype
The (standard) CL and LINK environment variables (which always get prepended respectively to the invocations of the compiler and the linker) should be set as follows:
set CL=/IC:\path\to\dir\containing\cairo.h /IC:\same\for\ft2build.h set LINK=/LIBPATH:C:\path\to\dir\containing\cairo.lib /LIBPATH:C:\same\for\freetype.lib
Moreover, we also need to find cairo.dll and freetype.dll and copy them next to mplcairo’s extension module. As the dynamic libraries are typically found next to import libraries, we search the /LIBPATH: entries in the LINK environment variable and copy the first cairo.dll and freetype.dll found there.
The script tools/build-windows-wheel.py automates the retrieval of the cairo (assuming that a Gohlke cairocffi is already installed) and FreeType and the wheel build.
Use
On Linux and Windows, mplcairo can be used as any normal Matplotlib backend: call e.g. matplotlib.use("module://mplcairo.qt") before importing pyplot, add a backend: module://mplcairo.qt line in your matplotlibrc, or set the MPLBACKEND environment variable to module://mplcairo.qt. More specifically, the following backends are provided:
module://mplcairo.base (No GUI, but can output to EPS, PDF, PS, SVG, and SVGZ using cairo’s implementation, rather than Matplotlib’s),
module://mplcairo.gtk (GTK3 widget, copying data from a cairo image surface),
module://mplcairo.gtk_native (GTK3 widget, directly drawn onto as a native surface; does not and cannot support blitting),
module://mplcairo.qt (Qt4/5 widget, copying data from a cairo image surface — select the binding to use by importing it before mplcairo, or by setting the QT_API environment variable),
module://mplcairo.tk (Tk widget, copying data from a cairo image surface),
module://mplcairo.wx (wx widget, copying data from a cairo image surface),
module://mplcairo.macosx (macOS widget, copying data from a cairo image surface).
On macOS, it is necessary to explicitly import mplcairo before importing Matplotlib due to incompatibilities associated with the use of a recent libc++. As such, the most practical option is to import mplcairo, then call e.g. matplotlib.use("module://mplcairo.macosx").
Alternatively, set the MPLCAIRO_PATCH_AGG environment variable to a non-empty value to fully replace the Agg renderer by the cairo renderer throughout Matplotlib. However, this approach is inefficient (due to the need of copies and conversions between premultiplied ARGB32 and straight RGBA8888 buffers); additionally, it does not work with the wx and macosx backends due to peculiarities of the corresponding canvas classes. On the other hand, this is currently the only way in which the webagg-based backends (e.g., Jupyter’s inline widget) are supported.
At import-time, mplcairo will attempt to load Raqm. The use of that library can be controlled and checked using the set_options and get_options functions.
The examples directory contains a few cases where the output of this renderer is arguably more accurate than the one of the default renderer, Agg:
circle_markers.py and square_markers.py: more accurate and faster marker stamping.
marker_stamping.py: more accurate marker stamping.
quadmesh.py: better antialiasing of quad meshes, fewer artefacts with masked data.
text_kerning.py: improved text kerning.
Benchmarks
Install (in the virtualenv) pytest>=3.1.0 and pytest-benchmark, then call (e.g.):
pytest --benchmark-group-by=fullfunc --benchmark-timer=time.process_time
Keep in mind that conda-forge’s cairo is (on my setup) ~2× slower than a “native” build of cairo.
Test suite
Run run-mpl-test-suite.py (which depends on pytest>=3.2.2) to run the Matplotlib test suite with the Agg backend patched by the mplcairo backend. Note that Matplotlib must be installed with its test data, which is not the case when it is installed from conda or from most Linux distributions; instead, it should be installed from PyPI or from source.
Nearly all image comparison tests “fail” as the renderers are fundamentally different; currently, the intent is to manually check the diff images. Passing --tolerance=inf marks these tests as “passed” (while still textually reporting the image differences) so that one can spot issues not related to rendering differences. In practice, --tolerance=50 appears to be enough.
Some other (non-image-comparison) tests are also known to fail (they are listed in ISSUES.rst, with the relevant explanations), and automatically skipped.
Run run-examples.py to run some examples that exercise some more aspects of mplcairo.
Notes
Antialiasing
The artist antialiasing property can be set to any of the cairo_antialias_t enum values, or True (the default) or False (which is synonym to NONE).
Setting antialiasing to True uses FAST antialiasing for lines thicker than 1/3px and BEST for lines thinner than that: for lines thinner than 1/3px, the former leads to artefacts such as lines disappearing in certain sections (see e.g. test_cycles.test_property_collision_plot after forcing the antialiasing to FAST). The threshold of 1/3px was determined empirically, see examples/thin_line_antialiasing.py.
Note that in order to set the lines.antialiased or patch.antialiased rcparams to a cairo_antialias_t enum value, it is necessary to bypass rcparam validation, using, e.g.
dict.__setitem__(plt.rcParams, "lines.antialiased", antialias_t.FAST)
The text.antialiased rcparam can likewise be set to any cairo_antialias_t enum value, or True (the default, which maps to SUBPIXEL — GRAY is not sufficient to benefit from Raqm’s subpixel positioning; see also cairo issue #152) or False (which maps to NONE).
Note that in rare cases, FAST antialiasing can trigger a “double free or corruption” bug in cairo (#44). If you hit this problem, consider using BEST or NONE antialiasing (depending on your quality and speed requirements).
Fast drawing
For fast drawing of path with many segments, the agg.path.chunksize rcparam should be set to e.g. 1000 (see examples/time_drawing_per_element.py for the determination of this value); this causes longer paths to be split into individually rendered sections of 1000 segments each (directly rendering longer paths appears to have slightly superlinear complexity).
Simplification threshold
The path.simplify_threshold rcparam is used to control the accuracy of marker stamping, down to an arbitrarily chosen threshold of 1/16px. If the threshold is set to a lower value, the exact (slower) marker drawing path will be used. Marker stamping is also implemented for scatter plots (which can have multiple colors). Likewise, markers of different sizes get mapped into markers of discretized sizes, with an error bounded by the threshold.
NOTE: pcolor and mplot3d’s plot_surface display some artifacts where the facets join each other. This is because these functions internally use a PathCollection, thus triggering the approximate stamping. pcolormesh (which internally uses a QuadMesh) should generally be preferred over pcolor anyways. plot_surface should likewise instead represent the surface using QuadMesh, which is drawn without such artefacts.
Font formats
In order to use a specific font that Matplotlib may be unable to use, pass a filename directly:
from matplotlib.font_manager import FontProperties
ax.text(.5, .5, "hello, world", fontproperties=FontProperties(fname="..."))
mplcairo still relies on Matplotlib’s font cache, so fonts unsupported by Matplotlib remain unavailable by other means.
For ttc fonts (and, more generally, font formats that include multiple font faces in a single file), the nth font (n≥0) can be selected by appending #n to the filename (e.g., fname="/path/to/font.ttc#1").
Note that Matplotlib’s (default) Agg backend will handle most (single-face) fonts equally well (ultimately, both backends relies on FreeType for rasterization). It is Matplotlib’s vector backends (PS, PDF, and, for pfb fonts, SVG) that do not support these fonts, whereas mplcairo support these fonts in all output formats.
Multi-page output
Matplotlib’s PdfPages class is deeply tied with the builtin backend_pdf (in fact, it cannot even be used with Matplotlib’s own cairo backend). Instead, use mplcairo.multipage.MultiPage for multi-page PDF and PS output. The API is similar:
from mplcairo.multipage import MultiPage
fig1 = ...
fig2 = ...
with MultiPage(path_or_stream, metadata=...) as mp:
mp.savefig(fig1)
mp.savefig(fig2)
See the class’ docstring for additional information.
cairo-script output
Setting the MPLCAIRO_SCRIPT_SURFACE environment variable before mplcairo is imported to vector or raster allows one to save figures (with savefig) in the .cairoscript format, which is a “native script that matches the cairo drawing model”. The value of the variable determines the rendering path used (e.g., whether marker stamping is used at all). This may be helpful for troubleshooting purposes.
Note that this may crash the process after the file is written, due to cairo issue #277.
Markers at Bézier control points
draw_markers draws a marker at each control point of the given path, which is the documented behavior, even though all builtin renderers only draw markers at straight or Bézier segment ends.
Known issues
Missing support from cairo
SVG output does not set URLs or ids on any element, as cairo provides no support to do so.
PS output does not respect SOURCE_DATE_EPOCH.
PS output does not support the Creator metadata key; however it supports the Title key.
The following rcparams have no effect:
pdf.fonttype (font type is selected by cairo internally),
pdf.inheritcolor (effectively always False),
pdf.use14corefonts (effectively always False),
ps.fonttype (font type is selected by cairo internally),
ps.useafm (effectively always False),
svg.fonttype (effectively always "path", see cairo issue #253),
svg.hashsalt.
Possible optimizations
Cache eviction policy and persistent cache for draw_path_collection.
Path simplification (although cairo appears to use vertex reduction and Douglas-Peucker internally?).
Use QtOpenGLWidget and the cairo-gl backend.
hexbin currently falls back on the slow implementation due to its use of the offset_position parameter. This should be fixed on Matplotlib’s side.
What about the already existing cairo (gtk3/qt4/qt5/wx/tk/…cairo) backends?
They are very slow (try running examples/mplot3d/wire3d_animation.py) and render math poorly (try title(r"$\sqrt{2}$")).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file mplcairo-0.2.tar.gz
.
File metadata
- Download URL: mplcairo-0.2.tar.gz
- Upload date:
- Size: 113.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b384429f80873d5ba728931fa118ed25b2a5a144847ff767b245c95f0a17c3a6 |
|
MD5 | 1c70daba57e3e2669e84d0537e322090 |
|
BLAKE2b-256 | 4f4005b259a7746dc9a5ef6438910ef5b3ab7e9110bb1a462ce895358bd028d4 |
File details
Details for the file mplcairo-0.2-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1accb1c0baa54909ee811b3fa8ec618f40fddf214fe8060fff77f097c5cc2345 |
|
MD5 | 75dd287f5432a14d2a1e25eda3f502ba |
|
BLAKE2b-256 | aafe5d66d0e0a6c4ceb6bad24077fc5d5f7594b002f3a49538747b389998da9d |
File details
Details for the file mplcairo-0.2-cp37-cp37m-manylinux1_x86_64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp37-cp37m-manylinux1_x86_64.whl
- Upload date:
- Size: 815.9 kB
- Tags: CPython 3.7m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03cacf3bb34d0832ce29657721cbd342dda4ef6f987c62b35df3cc28e6d59fc8 |
|
MD5 | 6cd9d160291316da2085b20ab02e79ba |
|
BLAKE2b-256 | 6d9bc5aee8db7be8d2ff6bf4735c6184d698ce9574e3f49d0dcf35ca9a702d5f |
File details
Details for the file mplcairo-0.2-cp37-cp37m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp37-cp37m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 299.9 kB
- Tags: CPython 3.7m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f69b8f14183f627f608ef8df1ca57b17f8436d717a861f5d6ffd56368313a5d7 |
|
MD5 | 5365ef4ee97cab41b4e27f1147f0780a |
|
BLAKE2b-256 | 6271344af4b2f546a0a5fa74e4761b080c0164b76bf25757e9d759a9935c7c14 |
File details
Details for the file mplcairo-0.2-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2bb83bdd6a1cd4b5c5e954b843bea322d658dc56e1fb93b2537d5ffbeac3db96 |
|
MD5 | 13ad880fc2064f94c3dacaaca9ad8f06 |
|
BLAKE2b-256 | 194755812358e50327dbc0d9b6616065a54aa11d5eb4a798f4d70014cca2e674 |
File details
Details for the file mplcairo-0.2-cp36-cp36m-manylinux1_x86_64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp36-cp36m-manylinux1_x86_64.whl
- Upload date:
- Size: 815.9 kB
- Tags: CPython 3.6m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | adee11a4213863f10e5b34e04e8df1a9010ff879b1f4dfd95c2aa2e0bac69021 |
|
MD5 | 8558a850d82361952eb17b5e110fefd2 |
|
BLAKE2b-256 | 7c3f17c51e757ef9d13b3dc1dafa29ad31a3b75cb0653a4b9c764f6fac9a4bb4 |
File details
Details for the file mplcairo-0.2-cp36-cp36m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp36-cp36m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 299.9 kB
- Tags: CPython 3.6m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 134a0bb4fbb7946d85f2339957d021fd6f64e35103fec9d8f82edbad2c797a0a |
|
MD5 | b90d60428d18093640d7cd6cb6b32465 |
|
BLAKE2b-256 | e4655d82385c4b0800e2590d9709b3a82c4fa8ee7008ae6e0dadd95dcdee6739 |
File details
Details for the file mplcairo-0.2-cp35-cp35m-manylinux1_x86_64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp35-cp35m-manylinux1_x86_64.whl
- Upload date:
- Size: 815.6 kB
- Tags: CPython 3.5m
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1426c3dabef11f27b2f8444391ccde0e47743fe1443077a4172f05722d78ecba |
|
MD5 | 583b02da6fcaacd43aa04a6f7ed5de58 |
|
BLAKE2b-256 | 822aefc63131f5e7f51e442be03c88dd765735873d0cb05fbdbaf3a810665cc5 |
File details
Details for the file mplcairo-0.2-cp35-cp35m-macosx_10_13_x86_64.whl
.
File metadata
- Download URL: mplcairo-0.2-cp35-cp35m-macosx_10_13_x86_64.whl
- Upload date:
- Size: 299.7 kB
- Tags: CPython 3.5m, macOS 10.13+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a35ff59a314c0d59d165f502c2d8b86b316f60900db954825fc0d7aa0fb07639 |
|
MD5 | 9e485c537b9b42825a1d2e5e1e741911 |
|
BLAKE2b-256 | 7ae9ea397d3077db063fe6ba9807e1720fc0eed804d5027f7ca4c76a651bc7e4 |