Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
amcpherson opened this issue Dec 2, 2022 · 10 comments · Fixed by #24783
Closed

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

amcpherson opened this issue Dec 2, 2022 · 10 comments · Fixed by #24783
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed topic: mpl_toolkit
Milestone

Comments

@amcpherson
Copy link

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

@jklymak
Copy link
Member

jklymak commented Dec 3, 2022

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

@anntzer
Copy link
Contributor

anntzer commented Dec 3, 2022

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)

@tacaswell tacaswell added this to the v3.6.3 milestone Dec 5, 2022
@tacaswell tacaswell added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! labels Dec 5, 2022
@tacaswell
Copy link
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

@oscargus oscargus added the status: has patch patch suggested, PR still needed label Dec 5, 2022
@matt256
Copy link
Contributor

matt256 commented Dec 8, 2022

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

@amcpherson
Copy link
Author

amcpherson commented Dec 8, 2022 via email

@oscargus
Copy link
Contributor

oscargus commented Dec 8, 2022

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

@matt256
Copy link
Contributor

matt256 commented Dec 9, 2022

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

@matt256
Copy link
Contributor

matt256 commented Dec 10, 2022

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?

@melissawm
Copy link
Contributor

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

@Gairick52
Copy link

@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
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed topic: mpl_toolkit
Projects
None yet
8 participants