Skip to content
gedouluhuiPublic

About

Source-assisted export evidence for Matplotlib figures: does the PDF match what the figure meant to draw?

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

PlotProof

Source-assisted export evidence for Matplotlib figures. PlotProof records what a figure intends to draw at save time, compares that record with the exported PDF, and reports what went wrong — or UNKNOWN when the evidence is not strong enough.

Status: B1 engineering preview (2026-11-19). The deterministic checker core, the bounded PDF reader and the CA1 save-time capture layer are implemented and tested. An experimental assembly runs the whole path end to end on one figure. It is not a certified detector: there is no holdout run and no accuracy number. D4 now uses bounded local font-cmap evidence; unavailable provenance is reported as UNKNOWN, never as a missing-glyph pass. Read What this does not do before trusting any output.

The problem

A Matplotlib figure can fail quietly:

  • a long axis label is clipped at the page edge;
  • a legend is drawn on top of data or text;
  • labels from different artists collide;
  • a CJK character has no glyph, so the PDF shows a blank or a box while the text layer still contains the original character.

Checking the PDF text layer alone cannot prove a glyph was drawn. Rasterizing alone cannot tell which pieces of text were meant to be there. PlotProof uses both: a source record captured inside Matplotlib at save time, and character boxes read from the PDF with a bounded PDFium worker.

Quick start

Requires Python 3.12 and Matplotlib 3.10.8 — the runtime the capture layer is version-checked against.

python -m venv .venv
.venv/Scripts/python -m pip install -e ".[dev]"     # Windows
# .venv/bin/python -m pip install -e ".[dev]"       # POSIX (the PDF worker is verified on Windows only)

.venv/Scripts/python -m plotproof demo --output plotproof-demo

The demo checks two generated figures and writes clean.{json,html} and clipped.{json,html}:

clean: PASS
clipped: FAIL

clipped contains a real finding — an annotation that starts inside the page and runs past its right edge:

{
  "rule_id": "D1",
  "defect_class": "clipped_text",
  "confidence": "high",
  "source_text": "This annotation runs past the edge",
  "box_pdf_points": [276.5, 114.2, 375.9, 124.3]
}

To check your own figure, point the CLI at a file that exposes a figure() function:

# make_figure.py
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

def figure():
    fig, ax = plt.subplots(figsize=(4, 3), dpi=100, layout="constrained")
    ax.bar(["alpha", "beta", "gamma"], [3, 5, 2])
    ax.set_xlabel("Category")
    ax.set_ylabel("Count", rotation=0)
    return fig
.venv/Scripts/python -m plotproof check make_figure.py --json report.json --html report.html --max-d3-work 1000000
# add --force only when intentionally replacing existing reports
# exit 0 for PASS/UNKNOWN, 1 for FAIL, 2 for ERROR reports or input/check/budget/output failures

Existing JSON/HTML files are refused unless --force is supplied. Destinations are preflighted before the figure module runs: symlinks, non-files/non-directory parents, ambiguous Windows trailing-dot/space/reserved components, input-module aliases (including hardlinks), JSON/HTML aliases, and collisions with <target>.plotproof-backup are rejected. Report text is staged before publication. Existing targets are backed up, then replaced in order; an ordinary publication failure rolls back both files. If rollback also fails, only the recovery backups that still exist are retained and reported for manual recovery. This is not crash atomicity: a process or power loss can leave a mixed pair. The demo uses the same policy and also requires --force to replace old evidence.

Structured CLI errors

Input, factory, check, budget and output failures return exit 2 and print one JSON object to stdout, without a traceback or private exception/path details:

{
  "status": "ERROR",
  "error": {"code": "INPUT_NOT_FOUND", "message": "The input module does not exist."}
}

When --json or --html names a safe destination, the error object (and a minimal escaped ERROR page) is published as a pair when possible. A failed error-publication attempt never hides the original error. Preflight/argument failures are stdout-only, so an alias refusal cannot overwrite the file it refused. User import/factory/check stdout and stderr are discarded at both Python and OS file-descriptor level, including child-process inheritance, to keep diagnostics parseable. KeyboardInterrupt is not converted. Other stable codes include FACTORY_NOT_FOUND, FACTORY_FAILED, IMPORT_FAILED, CHECK_FAILED, INVALID_ARGUMENTS, INVALID_BUDGET, OUTPUT_EXISTS, OUTPUT_ALIAS, INPUT_ALIAS, OUTPUT_SYMLINK, OUTPUT_BACKUP_EXISTS, OUTPUT_PUBLISH_FAILED and OUTPUT_ROLLBACK_FAILED.

The library form is the same call:

import plotproof

report = plotproof.check_figure(fig, max_d3_work=1_000_000)  # optional positive integer
print(report.state, report.assessed)

What is actually checked

ID Defect class Status in this preview
D1 clipped_text — a positive-area character box has at least half its area outside the page, or its centre strictly outside assessed
D2 occluded_text — one artist with α_eff ≥ 0.95, painted after the text, covers at least 90% of it assessed only when no image or collection was painted; otherwise reported as not assessed
D3 overlapping_text — two text objects' painted character boxes overlap enough to impair reading assessed unless its declared work budget is exceeded
D4 missing_glyph — a character is absent from every resolved font and Matplotlib warned about it assessed with bounded local cmap evidence; unavailable or malformed provenance is UNKNOWN

Every report states the classes it assessed and why the others were skipped:

{
  "state": "PASS",
  "assessed": ["D1_clipped_text", "D3_overlapping_text", "D4_missing_glyph"],
  "not_assessed": ["D2_occluded_text"],
  "capture_issues": ["collection_capture_pending", "unassociated_material_event"],
  "coverage": {"aligned_units": 13, "total_units": 13, "ratio": 1.0}
}

D3 complexity budget

check_figure(..., max_d3_work=N) and check --max-d3-work N accept a positive integer; the finite default is 1,000,000 declared operations. After alignment, PlotProof estimates all supported-unit pair work in O(n + fills):

pairs = n*(n-1)/2
operand_work = (n-1)*sum(c_i)
intersection_work = (sum(c_i)^2 - sum(c_i^2))/2
union_sort_work = (n-1)*sum(sort_work(c_i) + 2*sort_work(2*c_i))
cut_union_work = pairs*(sort_work(max(c_i)^2) + 2*sort_work(2*max(c_i)^2))
fill_work = pairs*(fill_count + sort_work(fill_count))
estimated_work = pairs + operand_work + intersection_work + union_sort_work
                 + cut_union_work + fill_work + 32*pairs

Here c_i is the aligned character-box count (or one source box when unaligned). This is a transparent operation budget, not an exact runtime prediction. If the estimate exceeds the limit, the entire D3 pair loop is skipped: no individual pairs are silently dropped. D1, D2 and D4 remain assessed; D4 reports explicit UNKNOWN evidence when font provenance is unavailable. D3 moves to not_assessed, a machine-readable complexity_budget record reports the components, pair count, limit, formula and skipped: true, and the state becomes UNKNOWN unless an existing ERROR or high-confidence FAIL correctly dominates. The boundary is inclusive.

Rectangle unions use an exact rational coordinate-compressed segment-tree sweep, O(m log m), rather than rescanning every box for every x strip. Rational coordinates avoid intermediate overflow, mixed-scale underflow and inf * 0; a true union area that cannot be represented as a finite, nonzero float raises ValueError instead of emitting NaN/Infinity or false zero evidence.

PASS means "no supported defect was found with sufficient coverage in the assessed classes". It is not a statement that the figure is correct, and it is not a benchmark result.

ERROR means the run could not make a statement at all: an unmapped unit inventory, an invalid event geometry, an ambiguous tight-crop origin, or a save outside the pinned runtime. UNKNOWN means the evidence exists but is not decisive: rotated text, math text, unresolved alignment, coverage below 0.90, or a finding at medium confidence.

What this does not do

  • No holdout, no accuracy claim. The benchmark manifest is frozen at B0; its holdout has never been generated or inspected. Nothing here is a precision/recall measurement.
  • Font evidence is bounded and local. D4 reads Unicode cmaps only from the renderer-selected font files, binds them by SHA-256, and treats missing, malformed or over-budget provenance as UNKNOWN. It does not certify font licensing, embedding, or every backend/version combination.
  • Rotated text is unsupported. Matplotlib's default vertical y-label forces UNKNOWN; use rotation=0 to get an assessment. Mathtext, TeX, wrapped text, rasterized text and path effects are also outside the supported set.
  • Collections and images limit D2. A plotted line's legend handle or a bar container is a collection; when one is painted, PlotProof cannot claim the paint inventory is complete and skips occlusion rather than guessing.
  • Not a general PDF checker. Arbitrary, multi-page, rotated or non-wrapper PDFs are rejected. It does not guess a crop: bbox_inches="tight" is supported only through the origin recorded during the save.
  • Not a semantic or aesthetic judge. It does not check whether the chart's message is true or whether the colours look good.
  • Verified on Windows, CPython 3.12.3, Matplotlib 3.10.8, PDFium 149.0.7825.0. The POSIX path of the PDF worker is not exercised by this release.

How it works

live Figure
  └─ capture.py (CA1)        temporary renderer hooks during the real savefig
       ├─ real PDF bytes + sha256
       ├─ draw-order events (sequence, PDF geometry, renderer-applied alpha)
       ├─ source inventory (Text/Patch traversal index, visibility, raw alpha)
       ├─ tight-crop origin (only when exactly one crop call is observed)
       └─ capture_issues (anything this layer could not map)
  └─ pdf_input.py            one owned process, hash-bound input, bounded output
       └─ page size, one character per PDF character with an unrounded box
  └─ units.py / alignment.py frozen §7.1 predicate, canonical form, greedy alignment
  └─ rules.py / core.py      D1–D4 with explicit evidence operands
  └─ export.py               assembly: records, fills, coverage, report

The checker is deterministic: float64 PDF points, no randomness, unrounded decisions. The 0.1 pt rounding happens only in the report.

Evidence and design record

  • docs/B0_PREREGISTRATION.md — v1.13, frozen before any detector code: input contract, defect definitions, §7.1 alignment, metrics, gates. Pinned by docs/B0_FREEZE.sha256 and tag b0-freeze-r17.
  • docs/reviews/B0_REVIEW.md — sixteen review rounds; rounds 8–15 returned REJECT, round 16 approved b0-freeze-r16 in an owner-run review (the independent dispatch hit its turn cap, so independence was not achieved).
  • docs/B1_IMPLEMENTATION.md and docs/B1_IO_IMPLEMENTATION.md — core and reader ledgers, including the measurement mistakes that were found and kept.
  • docs/CAPTURE_AMENDMENT_CA1.md — the amendment that governs capture, motivated by two reproduced counterexamples: a forced-alpha rectangle that renders at 0.6 and not the getter product 0.36, and a tight-crop bbox that differs from the Agg pre-draw.
  • benchmark/manifest.json — 24 templates, 228 samples, 192 defect instances, validated by benchmark/validate_manifest.py.
  • spikes/ — the feasibility probes, including the missing-glyph alignment measurement (U+20000, IoU 0.773) and the PDFium API spike (14/14).

Verified facts worth knowing before extending this:

  • with pdf.fonttype=42, CJK text and character boxes stay extractable;
  • with pdf.fonttype=3, ASCII survives but CJK does not, and U+2212 is dropped;
  • a missing glyph still appears in the text layer, so text extraction is not rendering evidence;
  • PDFium returns zero-area boxes for CR/LF and tick separators; D1 ignores them.

Roadmap

Milestone Content Status
B0 scope, defect definitions, sample inventory, holdout and metric freeze, licence audit, PDFium spike frozen (v1.13, tag b0-freeze-r17)
B1 deterministic core for D1–D4, bounded PDF reader, CA1 capture core/reader/capture implemented and tested
B1.5 end-to-end assembly, D1/D3 assessment, class-scoped reports this preview
B2 font evidence (D4), complete paint inventory (D2 for collections/images), dev-split calibration not started
B3 one sealed holdout run, preregistered report not started
B4 release, documentation, examples not started

Development

.venv/Scripts/python -m pip install -e ".[dev]"
python -m pytest              # run the full suite; see task report for verified totals
.venv/Scripts/python benchmark/validate_manifest.py

CI runs the suite on Ubuntu 3.12 and Windows 3.12 with Matplotlib 3.10.8.

License

MIT. Dependency and font licences are recorded in docs/DEPENDENCY_LICENSES.md and summarised in THIRD_PARTY_NOTICES.md. PyMuPDF/MuPDF is deliberately excluded because it is AGPL/commercial.

中文摘要

PlotProof 检查 Matplotlib 导出 PDF 时的静默失败:轴标签被页边裁掉、图例压住文字、 文字互相遮挡、中文缺字变成空白或方框。它靠两条证据:保存时在 Matplotlib 内部记录的 绘制意图,以及用受限 PDFium 子进程读出的字符框。

当前是 B1 工程预览:确定性核心、受限 PDF 读取、CA1 保存期采集都已实现并通过完整测试; 端到端装配可以真正跑通一张图,但不是认证过的检测器——没有保留集结果、没有 准确率数字;D4 使用有界的本地字体 cmap 证据,字体来源不可用时明确报告 UNKNOWN。

  • 已评估:D1 文字被裁、D3 文字重叠(未超出复杂度预算时)、D4 缺字(字体证据可用时);D2 遮挡仅在“没有图像/集合被绘制”时评估。
  • D4 不会把缺失、畸形或超预算的字体证据伪装成“没有缺字”。
  • 旋转文字(含默认竖排 y 轴标签)、数学公式、自动换行文字不支持,会强制 UNKNOWN。
  • PASS 只表示“在已评估类别中、覆盖率足够时没发现受支持的缺陷”,不代表图正确。
python -m plotproof demo --output plotproof-demo        # 干净图 PASS / 故意裁切图 FAIL
python -m plotproof demo --output plotproof-demo --force # 明确替换旧证据
python -m plotproof check make_figure.py --html report.html --max-d3-work 1000000

退出码:0 表示 PASS/UNKNOWN,1 表示 FAIL,2 表示 ERROR 报告或输入、工厂、检查、预算、输出错误。 错误以 stdout 中的结构化 JSON 返回;旧报告需显式 --force 才替换,相关回滚与 D3 预算语义 见上方“安全输出”“结构化 CLI 错误”和“D3 复杂度预算”。

Quickstart and support

Problem

Detect silent Matplotlib PDF export defects by combining save-time draw intent with bounded PDF character-box evidence.

Install

python -m pip install -e ".[dev]"

Success

python -m plotproof demo --output plotproof-demo

Expected: The generated clean figure passes and the deliberately clipped figure fails; the demo command exits 0 after verifying both controls.

Deliberate failure

python -m plotproof check does-not-exist.py --json missing-report.json --html missing-report.html

Expected: The missing figure module returns a structured stdout JSON error with exit 2 and no success is claimed. Existing outputs require explicit --force; safe error JSON is published when possible. --max-d3-work bounds all D3 pair evaluation.

Evidence and coverage boundaries

Evidence includes the full pytest suite for the deterministic core, bounded PDF reader, CA1 capture, structured CLI, safe publication, complexity budget, local font-cmap evidence and end-to-end assembly. There is no holdout run or accuracy number. D4 is assessed only when renderer-selected font provenance is available; otherwise it is explicitly UNKNOWN. PASS means only that no supported defect was found with sufficient coverage in assessed classes.

Support status

Version 0.1.0a4 is a B1 engineering preview for Python 3.12 and Matplotlib 3.10.8. The PDF worker path is verified on Windows only. It is not a certified detector and has no accuracy claim. Maintenance is best-effort with no SLA.

Limitations

Rotated text, mathtext, TeX, wrapped/rasterized text, path effects, arbitrary/multi-page/rotated PDFs, and semantic or aesthetic judgment are outside scope. D2 is skipped when images or collections prevent a complete paint inventory. D3 is skipped as a whole when its declared work budget is exceeded. Output rollback is not crash atomicity.

About

Source-assisted export evidence for Matplotlib figures: does the PDF match what the figure meant to draw?

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages