From 89fc97e30f5e11ffdf4f475dcb1d6ca14c99aa43 Mon Sep 17 00:00:00 2001 From: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> Date: Sat, 25 Apr 2026 00:47:38 +0600 Subject: [PATCH] Fix undefined `ax` variable in `plot_results` (#24345) Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> --- ultralytics/utils/plotting.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index c071ff74..8eec8976 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -876,6 +876,7 @@ def plot_results(file: str = "path/to/results.csv", dir: str = "", on_plot: Call assert len(files), f"No results.csv files found in {save_dir.resolve()}, nothing to plot." loss_keys, metric_keys = [], [] + fig, ax = None, None for i, f in enumerate(files): try: data = pl.read_csv(f, infer_schema_length=None) @@ -899,12 +900,13 @@ def plot_results(file: str = "path/to/results.csv", dir: str = "", on_plot: Call ax[i].set_title(j, fontsize=12) except Exception as e: LOGGER.error(f"Plotting error for {f}: {e}") - ax[1].legend() - fname = save_dir / "results.png" - fig.savefig(fname, dpi=200) - plt.close() - if on_plot: - on_plot(fname) + if ax is not None: + ax[1].legend() + fname = save_dir / "results.png" + fig.savefig(fname, dpi=200) + plt.close() + if on_plot: + on_plot(fname) def plt_color_scatter(v, f, bins: int = 20, cmap: str = "viridis", alpha: float = 0.8, edgecolors: str = "none"):