From bb5ebc290e5d630a081d7cbc5a9725ed8cea0a24 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Mon, 30 Aug 2021 17:22:21 +0200 Subject: [PATCH] Close `matplotlib` plots after opening (#4612) * Close plots * Replace fig.close() for plt.close() --- utils/metrics.py | 3 +++ utils/plots.py | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/utils/metrics.py b/utils/metrics.py index 44b9a3c..4f1b5e2 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -178,6 +178,7 @@ class ConfusionMatrix: fig.axes[0].set_xlabel('True') fig.axes[0].set_ylabel('Predicted') fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250) + plt.close() except Exception as e: print(f'WARNING: ConfusionMatrix plot failure: {e}') @@ -308,6 +309,7 @@ def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()): ax.set_ylim(0, 1) plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left") fig.savefig(Path(save_dir), dpi=250) + plt.close() def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'): @@ -328,3 +330,4 @@ def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence' ax.set_ylim(0, 1) plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left") fig.savefig(Path(save_dir), dpi=250) + plt.close() diff --git a/utils/plots.py b/utils/plots.py index eae295e..e470329 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -345,7 +345,6 @@ def profile_idetection(start=0, stop=0, labels=(), save_dir=''): a.remove() except Exception as e: print('Warning: Plotting error for %s; %s' % (f, e)) - ax[1].legend() plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200) @@ -371,6 +370,7 @@ def plot_evolve(evolve_csv=Path('path/to/evolve.csv')): # from utils.plots impo print('%15s: %.3g' % (k, mu)) f = evolve_csv.with_suffix('.png') # filename plt.savefig(f, dpi=200) + plt.close() print(f'Saved {f}') @@ -397,6 +397,7 @@ def plot_results(file='path/to/results.csv', dir=''): print(f'Warning: Plotting error for {f}: {e}') ax[1].legend() fig.savefig(save_dir / 'results.png', dpi=200) + plt.close() def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')): @@ -423,3 +424,4 @@ def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detec print(f'Saving {save_dir / f}... ({n}/{channels})') plt.savefig(save_dir / f, dpi=300, bbox_inches='tight') + plt.close()