diff --git a/efficalc/base_definitions/figure.py b/efficalc/base_definitions/figure.py index da3217f..37742b4 100644 --- a/efficalc/base_definitions/figure.py +++ b/efficalc/base_definitions/figure.py @@ -64,9 +64,9 @@ def __init__(self, figure, caption: str = None, full_width: bool = False): def load_image_data(self) -> bytes: """Loads the image data from the Matplotlib figure object as bytes.""" - tmp_file = BytesIO() - self.figure.savefig(tmp_file, format="png") - return tmp_file.getvalue() + with BytesIO() as tmp_file: + self.figure.savefig(tmp_file, format="png") + return tmp_file.getvalue() class FigureFromFile(FigureBase):