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"):