Fix multiple matplotlib windows with Agg backend interactive mode, caused duplicate graphs to be sent.

This commit is contained in:
allegroai 2019-09-27 13:22:17 +03:00
parent b720895307
commit 3eba23f1a3

View File

@ -134,7 +134,8 @@ class PatchedMatplotlib:
# store on the plot that this is an imshow plot # store on the plot that this is an imshow plot
stored_figure = _pylab_helpers.Gcf.get_active() stored_figure = _pylab_helpers.Gcf.get_active()
if stored_figure: if stored_figure:
stored_figure._trains_is_imshow = True stored_figure._trains_is_imshow = 1 if not hasattr(stored_figure, '_trains_is_imshow') \
else stored_figure._trains_is_imshow + 1
except Exception: except Exception:
pass pass
return ret return ret
@ -160,6 +161,8 @@ class PatchedMatplotlib:
try: try:
figures = PatchedMatplotlib._get_output_figures(None, all_figures=True) figures = PatchedMatplotlib._get_output_figures(None, all_figures=True)
for figure in figures: 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):
PatchedMatplotlib._report_figure(stored_figure=figure) PatchedMatplotlib._report_figure(stored_figure=figure)
except Exception: except Exception:
pass pass
@ -193,9 +196,10 @@ class PatchedMatplotlib:
# nothing for us to do # nothing for us to do
return return
# check if this is an imshow # check if this is an imshow
if hasattr(stored_figure, '_trains_is_imshow') and stored_figure._trains_is_imshow: if hasattr(stored_figure, '_trains_is_imshow'):
force_save_as_image = True
# flag will be cleared when calling clf() (object will be replaced) # 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
# get current figure # get current figure
mpl_fig = stored_figure.canvas.figure # plt.gcf() mpl_fig = stored_figure.canvas.figure # plt.gcf()
else: else: