Skip to content

Commit

Permalink
imgui_fig: import matplotlib only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Nov 20, 2024
1 parent f66c1c5 commit e66da2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
11 changes: 2 additions & 9 deletions bindings/imgui_bundle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,8 @@ def has_submodule(submodule_name):
#
# Import Python submodules
#

try:
# if matplotlib is not installed, we can't import imgui_fig
from imgui_bundle import imgui_fig as imgui_fig
__all__.extend(["imgui_fig"])
except ImportError:
pass


from imgui_bundle import imgui_fig as imgui_fig
__all__.extend(["imgui_fig"])


# Glfw setup:
Expand Down
36 changes: 21 additions & 15 deletions bindings/imgui_bundle/imgui_fig.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
matplotlib.use('Agg')
"""
from matplotlib.figure import Figure # noqa: E402
import numpy # noqa: E402
import matplotlib # noqa: E402
from imgui_bundle.immapp import static # noqa: E402
from imgui_bundle import immvision, ImVec2, imgui # noqa: E402


@static(fig_image_cache=dict())
def _fig_to_image(label_id: str, figure: Figure, refresh_image: bool = False) -> numpy.ndarray:
def _fig_to_image(label_id: str, figure: "matplotlib.figure.Figure", refresh_image: bool = False) -> numpy.ndarray:
"""
Convert a Matplotlib figure to an RGB image.
Expand All @@ -27,6 +25,19 @@ def _fig_to_image(label_id: str, figure: Figure, refresh_image: bool = False) ->
Returns:
- numpy.ndarray: An RGB image as a NumPy array with uint8 datatype.
"""
import matplotlib # noqa: E402

backend_message = """
imgui_fig.fig failed: in order to use imgui_fig, you need to change matplotlib renderer to Agg.
Add the following lines at the start of your script (and before importing pyplot):
import matplotlib
matplotlib.use('Agg')
"""

matplotlib_backend = matplotlib.rcParams['backend']
if matplotlib_backend.lower() != 'agg':
raise RuntimeError(backend_message)

statics = _fig_to_image
fig_id = imgui.get_id(label_id)
if refresh_image and fig_id in statics.fig_image_cache:
Expand All @@ -40,26 +51,21 @@ def _fig_to_image(label_id: str, figure: Figure, refresh_image: bool = False) ->

try:
buf.shape = (h, w, 3)
img = buf
matplotlib.pyplot.close(figure)
statics.fig_image_cache[fig_id] = img
except ValueError as e:
msg = """
imgui_fig.fig failed: in order to use imgui_fig, you need to change matplotlib renderer to Agg.
Add the following lines at the start of your script (and before importing pyplot):
import matplotlib
matplotlib.use('Agg')
"""
raise RuntimeError(msg) from e
raise RuntimeError(backend_message) from e
except Exception as e:
print(f"Error: {e}")

img = buf
matplotlib.pyplot.close(figure)
statics.fig_image_cache[fig_id] = img
return statics.fig_image_cache[fig_id]




def fig(label_id: str,
figure: matplotlib.figure.Figure,
figure: "matplotlib.figure.Figure",
size: ImVec2 | None = None,
refresh_image: bool = False,
resizable: bool = True,
Expand Down

0 comments on commit e66da2b

Please sign in to comment.