Source code for gemseo.utils.matplotlib_figure
# Copyright 2021 IRT Saint Exupéry, https://www.irt-saintexupery.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Services for handling Matplotlib figures, e.g. save and show."""
from __future__ import annotations
from typing import TYPE_CHECKING
import matplotlib.pyplot as plt
if TYPE_CHECKING:
from pathlib import Path
from matplotlib.figure import Figure
from gemseo.utils.file_path_manager import FilePathManager
FigSizeType = tuple[float, float]
"""The type of a figure size."""
[docs]
def save_show_figure(
fig: Figure,
show: bool,
file_path: str | Path,
fig_size: FigSizeType = (),
) -> None:
"""Save or show a Matplotlib figure.
Args:
fig: The Matplotlib figure to be saved or shown.
show: Whether to display the Matplotlib figure.
file_path: The file path to save the Matplotlib figure.
If empty, do not save the figure.
fig_size: The width and height of the figure in inches, e.g. ``(w, h)``.
If empty, use the current size of the figure.
"""
save = bool(file_path)
if fig_size:
fig.set_size_inches(fig_size)
if save:
fig.savefig(str(file_path), bbox_inches="tight")
if show:
plt.show()
if save:
plt.close(fig)
[docs]
def save_show_figure_from_file_path_manager(
fig: Figure,
file_path_manager: FilePathManager | None,
show: bool = False,
file_path: str | Path = "",
directory_path: str | Path = "",
file_name: str = "",
file_format: str = "",
fig_size: FigSizeType = (),
) -> Figure:
"""Save or show the plot.
Args:
fig: The figure to be processed.
file_path_manager: A manager of file paths
for a given type of file and with default settings.
If ``None``, do not save the figure.
show: Whether to show the figure.
file_path: The path of the file to save the figures.
If empty,
create a file path
from ``directory_path``, ``file_name`` and ``file_format``.
directory_path: The path of the directory to save the figures.
If empty, use the current working directory.
file_name: The name of the file to save the figures.
If empty, use a default one generated by the post-processing.
file_format: A file format, e.g. 'png', 'pdf', 'svg', ...
If empty, use a default file extension.
fig_size: The width and height of the figure in inches, e.g. ``(w, h)``.
If empty, use the current size of the figure.
Returns:
The figure.
"""
if file_path_manager is None:
file_path = ""
else:
file_path = file_path_manager.create_file_path(
file_path=file_path,
directory_path=directory_path,
file_name=file_name,
file_extension=file_format,
)
save_show_figure(fig, show, file_path, fig_size)
return fig