FigRecipe API Reference
figrecipe - Record and reproduce matplotlib figures.
A lightweight library for capturing matplotlib plotting calls and reproducing figures from saved recipes.
Usage
>>> import figrecipe as fr
>>> fig, ax = fr.subplots()
>>> ax.plot(x, y, id='my_data')
>>> fr.save(fig, 'recipe.yaml')
Examples
Recording a figure:
>>> import figrecipe as fr
>>> import numpy as np
>>>
>>> x = np.linspace(0, 10, 100)
>>> y = np.sin(x)
>>>
>>> fig, ax = fr.subplots()
>>> ax.plot(x, y, color='red', linewidth=2, id='sine_wave')
>>> ax.set_xlabel('Time')
>>> ax.set_ylabel('Amplitude')
>>> fr.save(fig, 'my_figure.yaml')
Reproducing a figure:
>>> fig, ax = fr.reproduce('my_figure.yaml')
>>> plt.show()
fr.utils: Unit conversions, font checks, low-level recipe access
fr.styles: Axis helpers, spine management, plot styling functions
fr.viz: Diagram and graph visualization utilities
>>> from figrecipe import utils
>>> utils.mm_to_inch(25.4) # Unit conversions
>>> from figrecipe import styles
>>> styles.hide_spines(ax) # Spine management
- class figrecipe.Figz(path)[source]
Bases:
objectMulti-panel figure bundle (.fig.zip).
- Manages a ZIP file containing:
manifest.json - bundle type declaration spec.json - figure spec with panel list style.json - figure dimensions and theme panels/ - .plt.zip panel bundles
Example
>>> figz = Figz.create("Figure1.fig.zip", "Figure1") >>> figz.add_panel("A", pltz_path) >>> figz.add_panel("B", pltz_bytes)
- add_panel(label, pltz_source, position=None, size=None)[source]
Add a .plt.zip panel to this figure.
- add_panel_from_png(label, png_bytes, plot_type='image', position=None, size=None, hitmap_bytes=None, hitmap_color_map=None, data_csv=None)[source]
Create a .plt.zip bundle from PNG bytes and embed it as a panel.
Convenience wrapper around
add_panel()for the common case of adding a pre-rendered PNG image as a panel.- Parameters:
label (str) – Panel label (e.g., “A”, “B”).
png_bytes (bytes) – PNG image data.
plot_type (str, optional) – Plot type label stored in the panel’s spec.json (default: “image”).
position (dict, optional) – Panel position e.g. {“x_mm”: 5, “y_mm”: 5}.
size (dict, optional) – Panel size e.g. {“width_mm”: 80, “height_mm”: 68}.
- Return type:
- get_panel(label)[source]
Extract panel as Pltz instance (via temp file).
Note: The caller is responsible for cleaning up the temp file (accessible via the returned Pltz instance’s .path attribute).
- class figrecipe.Pltz(path)[source]
Bases:
objectSingle-plot bundle (.plt.zip).
Thin wrapper around figrecipe’s existing bundle functions: - load_bundle() - save_bundle() - reproduce_bundle()
- Bundle structure (inside ZIP):
- {stem}/
spec.json # WHAT to plot (semantic specification) style.json # HOW it looks (appearance settings) data.csv # Raw data recipe.yaml # Reproducible recipe exports/
figure.png
- classmethod from_png(png_bytes, path, plot_type='image', spec=None, style=None, hitmap_bytes=None, hitmap_color_map=None, data_csv=None)[source]
Create a .plt.zip bundle wrapping a pre-rendered PNG image.
Useful when a figure is rendered externally (e.g., from a gallery template) and needs to be stored as a figrecipe bundle.
- Parameters:
- Returns:
Loaded Pltz instance wrapping the saved bundle.
- Return type:
- get_preview()[source]
Read pre-rendered preview PNG from bundle.
- Returns:
PNG bytes, or None if no preview image is stored.
- Return type:
bytes or None
- render_preview()[source]
Reproduce figure and render to PNG bytes.
- Returns:
PNG bytes, or None on failure.
- Return type:
bytes or None
- figrecipe.save_bundle(fig, path, dpi=None, image_formats=None, save_hitmap=False, verbose=True)[source]
Save figure as a layered bundle (ZIP format).
- Bundle structure inside ZIP:
spec.json # WHAT (semantic specification) style.json # HOW (appearance settings) data.csv # DATA (immutable source data) recipe.yaml # Reproducible recipe (for fr.reproduce()) exports/
figure.png figure_hitmap.png # only if save_hitmap=True
- Parameters:
fig (RecordingFigure) – The figure to save.
path (str or Path) – Output path (.zip will be added if not present).
dpi (int, optional) – DPI for exports (default from style or 300).
image_formats (list, optional) – Image formats to export (default: [‘png’]).
save_hitmap (bool) – Whether to save the per-pixel hitmap PNG used by the GUI editor for diff visualisation (default: False). Hitmap rendering is expensive and the typical save() caller doesn’t need it; opt in with save_hitmap=True when persisting bundles for the editor.
verbose (bool) – Whether to print status (default: True).
- Returns:
Path to saved ZIP bundle.
- Return type:
Path
- class figrecipe.Diagram(title=None, width_mm=170.0, height_mm=120.0, padding_mm=10.0, gap_mm=None)[source]
Bases:
objectBuilder for rich box-and-arrow diagrams with mm-based coordinates.
- add_box(id, title, subtitle=None, content=None, emphasis='normal', shape='rounded', x_mm=None, y_mm=None, width_mm=None, height_mm=None, fill_color=None, border_color=None, title_color=None, padding_mm=5.0, margin_mm=0.0, node_class=None, state=None, language=None, bullet=None)[source]
Add a rich text box. See BoxSpec for node_class/state/language/bullet.
- Return type:
- add_container(id, title=None, children=None, emphasis='muted', x_mm=None, y_mm=None, width_mm=None, height_mm=None, fill_color=None, border_color=None, title_loc='upper center', direction='row', container_gap_mm=8.0, container_padding_mm=8.0, equalize_heights=True, equalize_widths=True)[source]
Add a container. equalize_heights/widths: match children in row/column.
- Return type:
- add_arrow(source, target, source_anchor='auto', target_anchor='auto', source_dx=0.0, source_dy=0.0, target_dx=0.0, target_dy=0.0, label=None, style='solid', color=None, curve=0.0, linewidth_mm=0.5, label_offset_mm=None, margin_mm=None)[source]
Add an arrow connecting two boxes.
- Return type:
- add_icon(id, source, x_mm, y_mm, width_mm=8.0, height_mm=8.0, color=None, opacity=1.0)[source]
Add an icon (SVG/PNG or built-in: warning/check/cross/info/lock).
- Return type:
- validate_containers()[source]
Check every container fully encloses its declared children.
- Return type:
- auto_layout(layout='lr', margin_mm=15.0, box_size_mm=None, gap_mm=10.0, avoid_overlap=True, justify='space-between', align_items='center')[source]
Automatically position boxes. See _layout for details.
- Return type:
- _auto_box_height(box)[source]
Compute box height from content when height_mm is not specified.
- Return type:
- _finalize_canvas_size()[source]
Compute canvas height from element positions when height_mm=None.
- Return type:
- render(ax=None, auto_fix=False, auto_curve=True)[source]
Render. auto_fix=True resolves violations; auto_curve=False skips R7.
- save(path, dpi=200, save_recipe=True, save_hitmap=False, save_debug=True, watermark=False)[source]
Render, crop, and save. watermark=True adds ‘Plotted by scitex.ai’.
- Return type:
- render_to_file(path, dpi=200, save_recipe=True, save_hitmap=False, save_debug=True, watermark=False)
Render, crop, and save. watermark=True adds ‘Plotted by scitex.ai’.
- Return type:
- figrecipe.signature(image_path=None, recipe_path=None)[source]
Compute verification signature for a saved figure.
Core Functions
Recording Classes
These classes are available via from figrecipe import utils or from figrecipe._wrappers import RecordingFigure, RecordingAxes.
Style Management
Alignment Functions
GUI Editor
Diagram Class
- class figrecipe.Diagram(title=None, width_mm=170.0, height_mm=120.0, padding_mm=10.0, gap_mm=None)[source]
Builder for rich box-and-arrow diagrams with mm-based coordinates.
- add_box(id, title, subtitle=None, content=None, emphasis='normal', shape='rounded', x_mm=None, y_mm=None, width_mm=None, height_mm=None, fill_color=None, border_color=None, title_color=None, padding_mm=5.0, margin_mm=0.0, node_class=None, state=None, language=None, bullet=None)[source]
Add a rich text box. See BoxSpec for node_class/state/language/bullet.
- Return type:
- add_container(id, title=None, children=None, emphasis='muted', x_mm=None, y_mm=None, width_mm=None, height_mm=None, fill_color=None, border_color=None, title_loc='upper center', direction='row', container_gap_mm=8.0, container_padding_mm=8.0, equalize_heights=True, equalize_widths=True)[source]
Add a container. equalize_heights/widths: match children in row/column.
- Return type:
- add_arrow(source, target, source_anchor='auto', target_anchor='auto', source_dx=0.0, source_dy=0.0, target_dx=0.0, target_dy=0.0, label=None, style='solid', color=None, curve=0.0, linewidth_mm=0.5, label_offset_mm=None, margin_mm=None)[source]
Add an arrow connecting two boxes.
- Return type:
- add_icon(id, source, x_mm, y_mm, width_mm=8.0, height_mm=8.0, color=None, opacity=1.0)[source]
Add an icon (SVG/PNG or built-in: warning/check/cross/info/lock).
- Return type:
- validate_containers()[source]
Check every container fully encloses its declared children.
- Return type:
- auto_layout(layout='lr', margin_mm=15.0, box_size_mm=None, gap_mm=10.0, avoid_overlap=True, justify='space-between', align_items='center')[source]
Automatically position boxes. See _layout for details.
- Return type:
- _auto_box_height(box)[source]
Compute box height from content when height_mm is not specified.
- Return type:
- _finalize_canvas_size()[source]
Compute canvas height from element positions when height_mm=None.
- Return type:
- render(ax=None, auto_fix=False, auto_curve=True)[source]
Render. auto_fix=True resolves violations; auto_curve=False skips R7.
- save(path, dpi=200, save_recipe=True, save_hitmap=False, save_debug=True, watermark=False)[source]
Render, crop, and save. watermark=True adds ‘Plotted by scitex.ai’.
- Return type:
- render_to_file(path, dpi=200, save_recipe=True, save_hitmap=False, save_debug=True, watermark=False)
Render, crop, and save. watermark=True adds ‘Plotted by scitex.ai’.
- Return type: