chore: Finalize cleanup phases (docstrings, utility consolidation, dev app isolation)

This commit is contained in:
Yunxiao Xu
2026-02-17 02:50:08 -08:00
parent 1b15a4e18c
commit 16d8e81b6b
6 changed files with 46 additions and 41 deletions

View File

@@ -3,15 +3,25 @@ import matplotlib.pyplot as plt
def fig_to_bytes(fig: plt.Figure, format: str = "png") -> bytes:
"""
Convert a Matplotlib figure to bytes.
Convert a Matplotlib figure to a bytes object in the specified format.
This utility encapsulates the boilerplate logic for saving a figure to an
in-memory buffer and retrieving its binary content.
Args:
fig: The Matplotlib figure to convert.
format: The image format to use (default: "png").
fig: The Matplotlib Figure object to be converted.
format: The image format string (e.g., "png", "jpg", "svg").
Defaults to "png".
Returns:
bytes: The image data.
The binary image data as a bytes object.
Raises:
ValueError: If an unsupported image format is provided.
"""
buf = io.BytesIO()
fig.savefig(buf, format=format)
try:
fig.savefig(buf, format=format)
except Exception as e:
raise ValueError(f"Failed to convert figure to {format}: {str(e)}") from e
return buf.getvalue()