chore: Finalize cleanup phases (docstrings, utility consolidation, dev app isolation)
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user