$ show ErwindeGelder/matplot2tikz
matplot2tikz is a Python library that converts matplotlib figures into PGFPlots/TikZ code for native inclusion in LaTeX or ConTeXt documents. It recursively traverses a matplotlib Figure tree, translating each artist (lines, patches, images, colorbars, legends, text, etc.) into corresponding PGFPlots commands. The library provides two main entry points: `get_tikz_code()` returns the TikZ code as a string, and `save()` writes it to a file. It also includes a `clean_figure()` function that simplifies paths and reduces data points to minimize output size while preserving visual fidelity. The architecture is modular, with dedicated modules for each artist type (e.g., `_line2d.py`, `_patch.py`, `_axes.py`), a central data class (`TikzData`) to carry configuration and state, and a recursive traversal in `_save.py`. External dependencies include matplotlib, numpy, Pillow, and webcolors. The codebase is well-tested with over 170 test files and uses modern Python tooling (ruff, mypy, tox). It is a fork of the unmaintained tikzplotlib and is actively maintained with recent commits and CI.
The LLM identified 4 files worth highlighting for reuse:
Provides `_common_texification()` which escapes LaTeX special characters and handles math mode delimiters. It is a pure function that takes a string and returns a LaTeX-safe version, handling underscores, percent signs, ampersands, and the minus sign. This function is self-contained and can be extracted for any project needing to sanitize text for LaTeX output.
Implements the Opheim path simplification algorithm (`_opheim_simplify`) to reduce the number of data points in a line while preserving visual shape within a given tolerance. This is a pure function operating on numpy arrays and can be reused in any plotting or geometry simplification context. The module also contains helper functions for 3D projection and segment intersection.
Converts matplotlib color specifications (named colors, RGB tuples, RGBA) to LaTeX xcolor names or custom color definitions. It uses webcolors to find the closest named CSS3 color and generates `\definecolor` commands for non-standard colors. The function `mpl_color2xcolor` is the core, requiring only a dict-like object to store custom color definitions, making it easily extractable for other LaTeX generation tools.
Maps matplotlib marker styles to PGFPlots marker names using lookup dictionaries. Handles filled/unfilled markers and adds the 'plotmarks' library when needed. The function `_mpl_marker2pgfp_marker` is a pure mapping with minimal side effects (adding to a set of required libraries), making it reusable for any matplotlib-to-PGFPlots conversion.