-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: Error while creating inset axes using mpl_toolkits.axes_grid1.inset_locator.inset_axes
#26287
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
Comments
The problem here is that the inline backend (which is provided by IPython) is applying the The axes created by Now, when you just call Workarounds include:
|
Agreed - but it seems that |
Unfortunately it is still more complicated than simply setting While that gets a bit further, the I can update diff --git a/lib/mpl_toolkits/axes_grid1/inset_locator.py b/lib/mpl_toolkits/axes_grid1/inset_locator.py
index 9d35051074..1fdf99d573 100644
--- a/lib/mpl_toolkits/axes_grid1/inset_locator.py
+++ b/lib/mpl_toolkits/axes_grid1/inset_locator.py
@@ -69,6 +69,8 @@ class AnchoredLocatorBase(AnchoredOffsetbox):
raise RuntimeError("No draw method should be called")
def __call__(self, ax, renderer):
+ if renderer is None:
+ renderer = self.figure._get_renderer()
self.axes = ax
bbox = self.get_window_extent(renderer)
px, py = self.get_offset(bbox.width, bbox.height, 0, 0, renderer)
@@ -287,6 +289,7 @@ def _add_inset_axes(parent_axes, axes_class, axes_kwargs, axes_locator):
inset_axes = axes_class(
parent_axes.figure, parent_axes.get_position(),
**{"navigate": False, **axes_kwargs, "axes_locator": axes_locator})
+ axes_locator.figure = inset_axes.figure
return parent_axes.figure.add_axes(inset_axes) Setting the figure manually feels a little iffy, but so does calling And the renderer logic is also a little iffy as the figure is often still not set, so perhaps resolving that earlier in the call stack is a better idea? But regardless, it works, at least for |
The above looks close, but I don't think you need to plumb the figure in? Just do |
Ah, yes, that does make sense... it was a path function where I added that first but still had another failure, but I now see that I'm pretty sure the fix for the second will also fix the first, rendering the first fix moot. (and then yes, the iffiness of setting figure of an artist that is not actually in the axes goes away if you just get the |
This reverts commit f22fc9b. This is because the problem originates from matplotlib and not `mpl` method. It is fixed in matplotlib upstream and will be released with version 3.7.3. matplotlib/matplotlib#26287
Yes, since the fix was applied to the base class that both normal and zoomed inset_axes refer to subclasses of, it should fix that as well. |
Great, thanks! |
* data and analysis for PRL 130, 207001 (2023) * update noxfile * Pin matplotlib to prevent bug Prevents matplotlib/matplotlib#26287 * Pin sklearn --------- Co-authored-by: Andrey Antipov <andrey.antipov@microsoft.com> Co-authored-by: Bas Nijholt <bas@nijho.lt>
Looks like I broke this one with #25499. Sorry about that. |
Bug summary
Unable to create the inset axes in a plot using the code (following the first example on the website as posted here posted below.
Code for reproduction
Actual outcome
Expected outcome
I was expecting to add an empty box towards the top right of the first subplot (with axes
ax
) in the figure, as shown in the demo on the website.Additional information
No response
Operating system
Arch linux: 6.4.2-arch1-1
Matplotlib Version
3.7.2
Matplotlib Backend
module://matplotlib_inline.backend_inline
Python version
Python 3.8.17
Jupyter version
Jupyter lab: 3.6.5
Installation
conda
The text was updated successfully, but these errors were encountered: