Skip to content

Commit

Permalink
Support matplotlib backend "macosx" for CueFigure (#76)
Browse files Browse the repository at this point in the history
We do the same as for the inline backend.

Also improve the error message to suggest using "widget" backend.
  • Loading branch information
agoscinski authored Nov 29, 2024
1 parent 0b4dd2f commit 40d7b0f
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/scwidgets/cue/_widget_cue_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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 (
Expand All @@ -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 (
Expand All @@ -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):
Expand Down

0 comments on commit 40d7b0f

Please sign in to comment.