Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/zarr_benchmarks/plotting_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
from PIL import Image
from PIL.PngImagePlugin import PngInfo


def get_limits_custom(x_min: int, x_max: int, max_range: int) -> tuple[float, float]:
Expand Down Expand Up @@ -116,11 +118,9 @@ def add_error_bars(x, y, x_stddev, **kwargs):
title = title + " - 2 standard deviations errorbars"
graph.figure.suptitle(title)
graph.tight_layout()
alt_text = title

save_plot_as_png(
graph,
get_output_path(data, plots_dir, plot_name),
)
save_plot_as_png(graph, get_output_path(data, plots_dir, plot_name), alt_text)


def plot_relplot_benchmarks(
Expand Down Expand Up @@ -195,11 +195,9 @@ def plot_relplot_benchmarks(
if title is not None:
graph.figure.suptitle(title)
graph.tight_layout()
alt_text = title

save_plot_as_png(
graph,
get_output_path(data, plots_dir, plot_name),
)
save_plot_as_png(graph, get_output_path(data, plots_dir, plot_name), alt_text)


def get_axis_labels(
Expand Down Expand Up @@ -229,11 +227,19 @@ def get_output_path(
return plots_dir / f"{plot_name}_{date}_{machine_info}.png"


def save_plot_as_png(grid: sns.FacetGrid, output_path: Path) -> None:
def save_plot_as_png(
grid: sns.FacetGrid, output_path: Path, alt_text: str | None = None
) -> None:
output_path.parent.mkdir(parents=True, exist_ok=True)
grid.savefig(output_path, format="png", dpi=300)
plt.close()

if alt_text:
img = Image.open(output_path)
metadata = PngInfo()
metadata.add_text("Alt", alt_text)
img.save(output_path, pnginfo=metadata)


def plot_catplot_benchmarks(
data: pd.DataFrame,
Expand Down Expand Up @@ -283,8 +289,6 @@ def plot_catplot_benchmarks(
if title is not None:
graph.figure.suptitle(title)
graph.tight_layout()
alt_text = title

save_plot_as_png(
graph,
get_output_path(data, plots_dir, plot_name),
)
save_plot_as_png(graph, get_output_path(data, plots_dir, plot_name), alt_text)