SVG Export Helper
GitHub Link to Code.
SVG export helper for editable text and large-plot export stability.
Provides utilities that keep SVG text editable and apply save-time rendering heuristics for very large artists to reduce vector export cost.
- class mdxplain.plots.helper.svg_export_helper.SvgExportHelper
Helper for plot export configuration and optimizations.
Centralizes save-time behavior for plot outputs:
keep SVG text editable (svg.fonttype=’none’)
selectively rasterize very heavy artists in vector outputs
temporarily tune path rcParams for very large polylines
Examples
>>> SvgExportHelper.apply_svg_config_if_needed("svg") >>> fig.savefig("plot.svg", format="svg") >>> SvgExportHelper.save_figure_with_export_optimizations( ... fig=fig, filepath="plot.svg", file_format="svg", dpi=300 ... )
- static configure_svg_text_editability() None
Configure matplotlib for editable SVG text export.
Sets matplotlib rcParams to prevent text-to-path conversion in SVG exports. After calling this method, all text elements in exported SVG files will be selectable and editable in SVG editors.
Parameters
None
Returns
- None
Modifies global matplotlib rcParams
Notes
Sets ‘svg.fonttype’ to ‘none’ to preserve text as <text> elements
This is a global setting that affects all subsequent SVG exports
To restore default behavior, call restore_default_svg_settings()
Examples
>>> # Configure once at beginning >>> SvgExportHelper.configure_svg_text_editability() >>> fig.savefig("plot1.svg", format="svg") >>> fig2.savefig("plot2.svg", format="svg")
- static restore_default_svg_settings() None
Restore default matplotlib SVG export settings.
Resets SVG font configuration to matplotlib defaults, which converts text to paths for better portability but reduced editability.
Parameters
None
Returns
- None
Modifies global matplotlib rcParams
Examples
>>> # Temporarily use editable text >>> SvgExportHelper.configure_svg_text_editability() >>> fig.savefig("editable.svg", format="svg") >>> >>> # Restore defaults for other exports >>> SvgExportHelper.restore_default_svg_settings() >>> fig.savefig("paths.svg", format="svg")
- static get_current_svg_settings() Dict[str, str]
Get current SVG export settings.
Returns a dictionary of current SVG-related rcParams settings.
Parameters
None
Returns
- Dict[str, str]
Dictionary with current SVG settings
Examples
>>> settings = SvgExportHelper.get_current_svg_settings() >>> print(settings['svg.fonttype']) 'none'
- static apply_svg_config_if_needed(file_format: str) None
Apply SVG configuration if format is SVG.
Convenience method that checks the file format and applies editable text configuration only if the format is ‘svg’.
Parameters
- file_formatstr
The export file format (e.g., ‘svg’, ‘png’, ‘pdf’)
Returns
- None
Conditionally modifies matplotlib rcParams
Examples
>>> # Automatically configure based on format >>> SvgExportHelper.apply_svg_config_if_needed("svg") # Applies config >>> SvgExportHelper.apply_svg_config_if_needed("png") # Does nothing
- static save_figure_with_export_optimizations(fig: Figure, filepath: str | Path, file_format: str, dpi: int, bbox_inches: str = 'tight') None
Save figure with export-time performance optimizations.
Applies optional, human-equivalent optimizations for large plots:
editable text config for SVG output
selective rasterization of very heavy artists on vector backends
temporary path rcParam tuning for very large polylines
Parameters
- figmatplotlib.figure.Figure
Figure to save.
- filepathstr or Path
Output file path.
- file_formatstr
Export format.
- dpiint
Output resolution in dots per inch.
- bbox_inchesstr, default=”tight”
Bounding box mode forwarded to
Figure.savefig.
Returns
- None
Figure is written to disk.
Notes
Temporary artist and rcParam changes are restored in
finally.