Matplotlib generates SVG plots using hundreds of piecewise line segments, resulting in unnecessarily large files. Using an orthogonal distance fitting (ODF) algorithm with cubic Bézier curves and Chebyshev series approximation, the same plot can be represented with just 8 Bézier segments — visually identical but far more compact. The post walks through the fitting algorithm (Algorithm F), the Bézier curve optimizer (Algorithm B using a trust-region optimizer), and the max-distance computation (Algorithm M). It also shows how to convert the resulting TikZ path commands into SVG images using MetaPost, with examples including parametric functions and annotated axes. A Python package called bbai implements the algorithm.
Table of contents
1. Introduction2. Functionality Tour3. The Fitting Algorithm4. The Max Distance Algorithm5. How to Produce SVG Images6. Benchmarks7. ConclusionsReferences and NotesSort: