Skip to content

[Bug]: inset_locator is broken when used with subfigures #24589

Closed

Description

@amcpherson

Bug summary

When using inset_locator functions in subfigures, the plots are not placed correctly. Adapting this demo
https://matplotlib.org/stable/gallery/axes_grid1/demo_colorbar_of_inset_axes.html
to use subfigures shows incorrect plots.

Code for reproduction

from matplotlib import cbook
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes


fig = plt.figure()
fig1, fig2 = fig.subfigures(nrows=2, ncols=1, height_ratios=[1, 1], squeeze=True)

ax = fig1.subplots()
ax.set(aspect=1, xlim=(-15, 15), ylim=(-20, 5))

Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy", np_load=True)
extent = (-3, 4, -4, 3)

axins = zoomed_inset_axes(ax, zoom=2, loc='upper left')
axins.set(xticks=[], yticks=[])
im = axins.imshow(Z, extent=extent, origin="lower")

# colorbar
cax = inset_axes(axins,
                 width="5%",  # width = 10% of parent_bbox width
                 height="100%",  # height : 50%
                 loc='lower left',
                 bbox_to_anchor=(1.05, 0., 1, 1),
                 bbox_transform=axins.transAxes,
                 borderpad=0,
                 )
fig.colorbar(im, cax=cax)

plt.show()

Actual outcome

image

Expected outcome

Something more similar to https://matplotlib.org/stable/gallery/axes_grid1/demo_colorbar_of_inset_axes.html

Additional information

No response

Operating system

OS/X

Matplotlib Version

3.6.1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

No response

Jupyter version

No response

Installation

pip

Activity

  1. jklymak commented on Dec 3, 2022

    @jklymak
    Member

    That seems likely as axes_grid1 is not very subfigure-aware. Did you try with ax.inset_axes instead?

  2. anntzer commented on Dec 3, 2022

    @anntzer
    Contributor

    From a quick try, the fix seems to be simply

    diff --git i/lib/mpl_toolkits/axes_grid1/inset_locator.py w/lib/mpl_toolkits/axes_grid1/inset_locator.py
    index bb90f8273c..0771efd71f 100644
    --- i/lib/mpl_toolkits/axes_grid1/inset_locator.py
    +++ w/lib/mpl_toolkits/axes_grid1/inset_locator.py
    @@ -73,7 +73,7 @@ class AnchoredLocatorBase(AnchoredOffsetbox):
             bbox = self.get_window_extent(renderer)
             px, py = self.get_offset(bbox.width, bbox.height, 0, 0, renderer)
             bbox_canvas = Bbox.from_bounds(px, py, bbox.width, bbox.height)
    -        tr = ax.figure.transFigure.inverted()
    +        tr = ax.figure.transSubfigure.inverted()
             return TransformedBbox(bbox_canvas, tr)
  3. added this to the v3.6.3 milestone on Dec 5, 2022
  4. added
    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Dec 5, 2022
  5. tacaswell commented on Dec 5, 2022

    @tacaswell
    Member

    Labeling this as easy and good first issue because there is a proposed patch and a clear reproduction case.

    The tasks here:

    • verify the proposed patch fixes the issue and commit it
    • write a test (based on the bug report) This may be a bit tricky to do without adding an image test and I suspect be a vast majority of the work on this issue
  6. matt256 commented on Dec 8, 2022

    @matt256
    Contributor

    mind if I grab this one? Long time user. Would love to be a first time contributor

  7. amcpherson commented on Dec 8, 2022

    @amcpherson
    Author
  8. oscargus commented on Dec 8, 2022

    @oscargus
    Member

    Please go ahead @matt256 ! We do not assign issues though.

  9. matt256 commented on Dec 9, 2022

    @matt256
    Contributor

    Understood. Thank you and will do. Fix confirmed. Working on the test

  10. matt256 commented on Dec 10, 2022

    @matt256
    Contributor

    PR submitted, but i had some issues with the styling of the test image through the decorator not being classic so i just made the comparison image in the classic style. Naturally i figured out i had to change it to mpl20 as soon as I submitted the PR. I assume i can push another commit to a PR?

  11. melissawm commented on Dec 11, 2022

    @melissawm
    Member

    Sure @matt256, you can update your PR as needed. If you need help feel free to ping 😄

  12. linked a pull request that will close this issueinset locator fix with tests added #24783on Dec 20, 2022
  13. Gairick52 commented on Jan 10, 2023

    @Gairick52

    @amcpherson Hello sir,i want to work on this issue,please assign this to me

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

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesstatus: has patchpatch suggested, PR still neededtopic: mpl_toolkit🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions