diff --git a/clearml/binding/matplotlib_bind.py b/clearml/binding/matplotlib_bind.py index f670e971..06e52564 100644 --- a/clearml/binding/matplotlib_bind.py +++ b/clearml/binding/matplotlib_bind.py @@ -195,7 +195,7 @@ class PatchedMatplotlib: # store on the plot that this is an imshow plot stored_figure = _pylab_helpers.Gcf.get_active() if stored_figure: - stored_figure._trains_is_imshow = 1 if not hasattr(stored_figure, '_trains_is_imshow') \ + stored_figure._trains_is_imshow = 1 if getattr(stored_figure, '_trains_is_imshow', None) is None \ else stored_figure._trains_is_imshow + 1 except Exception: pass @@ -253,7 +253,8 @@ class PatchedMatplotlib: figures = PatchedMatplotlib._get_output_figures(None, all_figures=True) for figure in figures: # if this is a stale figure (just updated) we should send it, the rest will not be stale - if figure.canvas.figure.stale or (hasattr(figure, '_trains_is_imshow') and figure._trains_is_imshow): + if figure.canvas.figure.stale or ( + getattr(figure, '_trains_is_imshow', None) is not None and figure._trains_is_imshow): PatchedMatplotlib._report_figure(stored_figure=figure) except Exception: pass @@ -263,6 +264,14 @@ class PatchedMatplotlib: # noinspection PyBroadException try: if sys.modules['matplotlib'].rcParams['backend'] == 'agg': + # noinspection PyBroadException + try: + from matplotlib import _pylab_helpers + stored_figure = _pylab_helpers.Gcf.get_active() + stored_figure._trains_is_imshow = None + stored_figure.canvas.figure._trains_explicit = False + except Exception: + pass import matplotlib.pyplot as plt plt.clf() except Exception: @@ -324,7 +333,7 @@ class PatchedMatplotlib: # nothing for us to do return # check if this is an imshow - if hasattr(stored_figure, '_trains_is_imshow'): + if getattr(stored_figure, '_trains_is_imshow', None) is not None: # flag will be cleared when calling clf() (object will be replaced) stored_figure._trains_is_imshow = max(0, stored_figure._trains_is_imshow - 1) force_save_as_image = True