Skip to content

Error when trying to add inset axis #27

Closed

Description

@smups

Info:

  • python 3.13
  • ultraplot 1.0
  • matplotlib 3.10.0
  • OS: fedora 41 (x86_64)

Code that breaks:

fig = uplt.figure(figwidth=6.2, share=False, ref=4)
axs = fig.subplots([[1,2,3],[4,5,6],[4,5,6]])
fig.format(toplabels=['Model A', 'Model B', 'Model C']

m = axs[0,0].imshow(np.transpose(a), extent=extent_a)
axs[1,0].plot(*trajectory_a)

axs[1,1].imshow(np.transpose(b), extent=extent_b)
axs[0,1].plot(*trajectory_b)

# extent_c = (0, 35, 0, 4π ≈ 12)
axs[1,2].imshow(np.transpose(c), extent=extent_b)
axs[0,2].plot(*trajectory_c)

# (code works without this line)
ix = axs[1,2].inset([5, 5, 5, 5]) # <- error: `NotImplementedError: cannot remove artist`

fig.colorbar(m, loc='r', extend='max', formatter='sci')

Complete error:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
File ~/.local/lib/python3.13/site-packages/matplotlib/pyplot.py:279, in _draw_all_if_interactive()
    277 def _draw_all_if_interactive() -> None:
    278     if matplotlib.is_interactive():
--> 279         draw_all()

File ~/.local/lib/python3.13/site-packages/matplotlib/_pylab_helpers.py:131, in Gcf.draw_all(cls, force)
    129 for manager in cls.get_all_fig_managers():
    130     if force or manager.canvas.figure.stale:
--> 131         manager.canvas.draw_idle()

File ~/.local/lib/python3.13/site-packages/matplotlib/backend_bases.py:1891, in FigureCanvasBase.draw_idle(self, *args, **kwargs)
   1889 if not self._is_idle_drawing:
   1890     with self._idle_draw_cntx():
-> 1891         self.draw(*args, **kwargs)

File ~/.local/lib/python3.13/site-packages/ultraplot/figure.py:467, in _add_canvas_preprocessor.<locals>._canvas_preprocess(self, *args, **kwargs)
    465 ctx3 = rc.context(fig._render_context)  # draw with figure-specific setting
    466 with ctx1, ctx2, ctx3:
--> 467     fig.auto_layout()
    468     return func(self, *args, **kwargs)

File ~/.local/lib/python3.13/site-packages/ultraplot/figure.py:1562, in Figure.auto_layout(self, renderer, aspect, tight, resize)
   1560 _align_content()
   1561 if tight:
-> 1562     gs._auto_layout_tight(renderer)
   1563 _align_content()

File ~/.local/lib/python3.13/site-packages/ultraplot/gridspec.py:848, in GridSpec._auto_layout_tight(self, renderer)
    846 pad = self._outerpad
    847 obox = fig.bbox_inches  # original bbox
--> 848 bbox = fig.get_tightbbox(renderer)
    850 # Calculate new figure margins
    851 # NOTE: Negative spaces are common where entire rows/columns of gridspec
    852 # are empty but it seems to result in wrong figure size + grid positions. Not
    853 # worth correcting so instead enforce positive margin sizes. Will leave big
    854 # empty slot but that is probably what should happen under this scenario.
    855 left = self.left

File ~/.local/lib/python3.13/site-packages/matplotlib/figure.py:1848, in FigureBase.get_tightbbox(self, renderer, bbox_extra_artists)
   1844 if ax.get_visible():
   1845     # some Axes don't take the bbox_extra_artists kwarg so we
   1846     # need this conditional....
   1847     try:
-> 1848         bbox = ax.get_tightbbox(
   1849             renderer, bbox_extra_artists=bbox_extra_artists)
   1850     except TypeError:
   1851         bbox = ax.get_tightbbox(renderer)

File ~/.local/lib/python3.13/site-packages/ultraplot/axes/cartesian.py:1417, in CartesianAxes.get_tightbbox(self, renderer, *args, **kwargs)
   1415 self._apply_axis_sharing()
   1416 self._update_rotation("x")
-> 1417 return super().get_tightbbox(renderer, *args, **kwargs)

File ~/.local/lib/python3.13/site-packages/ultraplot/axes/base.py:2838, in Axes.get_tightbbox(self, renderer, *args, **kwargs)
   2836 if self._inset_parent is not None and self._inset_zoom:
   2837     self.indicate_inset_zoom()
-> 2838 self._tight_bbox = super().get_tightbbox(renderer, *args, **kwargs)
   2839 return self._tight_bbox

File ~/.local/lib/python3.13/site-packages/matplotlib/axes/_base.py:4542, in _AxesBase.get_tightbbox(self, renderer, call_axes_locator, bbox_extra_artists, for_layout_only)
   4539     bbox_artists = self.get_default_bbox_extra_artists()
   4541 for a in bbox_artists:
-> 4542     bbox = a.get_tightbbox(renderer)
   4543     if (bbox is not None
   4544             and 0 < bbox.width < np.inf
   4545             and 0 < bbox.height < np.inf):
   4546         bb.append(bbox)

File ~/.local/lib/python3.13/site-packages/ultraplot/axes/cartesian.py:1417, in CartesianAxes.get_tightbbox(self, renderer, *args, **kwargs)
   1415 self._apply_axis_sharing()
   1416 self._update_rotation("x")
-> 1417 return super().get_tightbbox(renderer, *args, **kwargs)

File ~/.local/lib/python3.13/site-packages/ultraplot/axes/base.py:2837, in Axes.get_tightbbox(self, renderer, *args, **kwargs)
   2835     self._colorbar_fill.update_ticks(manual_only=True)  # only if needed
   2836 if self._inset_parent is not None and self._inset_zoom:
-> 2837     self.indicate_inset_zoom()
   2838 self._tight_bbox = super().get_tightbbox(renderer, *args, **kwargs)
   2839 return self._tight_bbox

File ~/.local/lib/python3.13/site-packages/ultraplot/axes/base.py:2903, in Axes.indicate_inset_zoom(self, **kwargs)
   2901 rectpatch.update_from(rectpatch_prev)
   2902 rectpatch.set_zorder(rectpatch_prev.get_zorder())
-> 2903 rectpatch_prev.remove()
   2904 for line, line_prev in zip(connects, connects_prev):
   2905     line.update_from(line_prev)

File ~/.local/lib/python3.13/site-packages/matplotlib/artist.py:256, in Artist.remove(self)
    253         self._parent_figure = None
    255 else:
--> 256     raise NotImplementedError('cannot remove artist')

NotImplementedError: cannot remove artist

Activity

  1. cvanelteren commented on Jan 24, 2025

    @cvanelteren
    Collaborator

    Hi thanks for the report. Can you please checkout main? It should work fine there:

    pip install +git:https://github.com/Ultraplot/ultraplot.git
    
  2. cvanelteren commented on Jan 26, 2025

    @cvanelteren
    Collaborator

    I see this issue pops up after 3.9.3 -- will have a look thanks!

  3. cvanelteren commented on Jan 26, 2025

    @cvanelteren
    Collaborator

    I would suggest downgrade mpl until I can fix this -- apologies for the inconvenience!

  4. cvanelteren commented on Jan 26, 2025

    @cvanelteren
    Collaborator

    Strange. I don't see any api changes for remove_artist. I can also verify that for inset axis a remove_method is set, which should prevent hitting the error.

    Relevant mpl code is: https://github.com/matplotlib/matplotlib/blob/3ac0aea8be540028cb2510dabb43b290d15b503d/lib/matplotlib/artist.py#L221

  5. cvanelteren commented on Jan 26, 2025

    @cvanelteren
    Collaborator

    Ah I am on the right track now. Will fix! Thanks for reporting

  6. cvanelteren commented on Jan 26, 2025

    @cvanelteren
    Collaborator

    The issue is mpl 3.10 introduced InsetIndicator. I tried to just provide a mapping between both but it is not as trivial as I thought. Will give it some thought this week to see how to fix this.

  7. cvanelteren commented on Jan 27, 2025

    @cvanelteren
    Collaborator

    Image

    we are getting somewhere ;-)

  8. cvanelteren commented on Jan 27, 2025

    @cvanelteren
    Collaborator

    Another success

    Image

  9. cvanelteren commented on Jan 27, 2025

    @cvanelteren
    Collaborator

    Image
    🥳

  10. smups commented on Jan 27, 2025

    @smups
    Author

    wow thx for all the work!!

  11. cvanelteren commented on Jan 27, 2025

    @cvanelteren
    Collaborator

    Fixed on main. Will push a new tag. Update with pypi or conda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions