From 40d7b0f9d2847bac3503ae3ff1cfe2e53aae8b00 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Fri, 29 Nov 2024 15:14:41 +0100 Subject: [PATCH] Support matplotlib backend "macosx" for `CueFigure` (#76) We do the same as for the inline backend. Also improve the error message to suggest using "widget" backend. --- src/scwidgets/cue/_widget_cue_figure.py | 27 +++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/scwidgets/cue/_widget_cue_figure.py b/src/scwidgets/cue/_widget_cue_figure.py index 97ba4f3..dc12a81 100644 --- a/src/scwidgets/cue/_widget_cue_figure.py +++ b/src/scwidgets/cue/_widget_cue_figure.py @@ -64,7 +64,10 @@ def __init__( ) self.figure = figure - if matplotlib.backends.backend == "module://matplotlib_inline.backend_inline": + if matplotlib.backends.backend in [ + "module://matplotlib_inline.backend_inline", + "macosx", + ]: # we close the figure so the figure is only contained in this widget # and not shown using plt.show() plt.close(self.figure) @@ -78,7 +81,9 @@ def __init__( self.figure.canvas.show() else: raise NotImplementedError( - f"matplotlib backend " f"{matplotlib.backends.backend!r} not supported." + f"matplotlib backend {matplotlib.backends.backend!r} not supported. " + "Please change backend to 'widget' by running `%matplotlib widget` " + "that should be supported on all systems." ) if no_toolbars: @@ -96,7 +101,10 @@ def clear_display(self, wait=False): same meaning as for the `wait` parameter in the ipywidgets.clear_output function """ - if matplotlib.backends.backend == "module://matplotlib_inline.backend_inline": + if matplotlib.backends.backend in [ + "module://matplotlib_inline.backend_inline", + "macosx", + ]: self.clear_figure() self.clear_output(wait=wait) elif ( @@ -111,14 +119,19 @@ def clear_display(self, wait=False): self.figure.canvas.flush_events() else: raise NotImplementedError( - f"matplotlib backend " f"{matplotlib.backends.backend!r} not supported." + f"matplotlib backend {matplotlib.backends.backend!r} not supported. " + "Please change backend to 'widget' by running `%matplotlib widget` " + "that should be supported on all systems." ) def draw_display(self): """ Enforces redrawing the figure """ - if matplotlib.backends.backend == "module://matplotlib_inline.backend_inline": + if matplotlib.backends.backend in [ + "module://matplotlib_inline.backend_inline", + "macosx", + ]: with self: display(self.figure) elif ( @@ -131,7 +144,9 @@ def draw_display(self): self.figure.canvas.flush_events() else: raise NotImplementedError( - f"matplotlib backend " f"{matplotlib.backends.backend!r} not supported." + f"matplotlib backend {matplotlib.backends.backend!r} not supported. " + "Please change backend to 'widget' by running `%matplotlib widget` " + "that should be supported on all systems." ) def clear_figure(self):