Skip to content

Add Inset Zoom Axes with Automatic Plotting of Parent Axes Artists. #22060

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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/axes_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Text and annotations
Axes.table
Axes.arrow
Axes.inset_axes
Axes.inset_zoom_axes
Axes.indicate_inset
Axes.indicate_inset_zoom
Axes.secondary_xaxis
Expand Down
25 changes: 25 additions & 0 deletions doc/users/next_whats_new/autoplot_inset_axes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Addition of an inset Axes with automatic zoom plotting
------------------------------------------------------

It is now possible to create an inset axes that is a zoom-in on a region in
the parent axes without needing to replot all items a second time, using the
`~matplotlib.axes.Axes.inset_zoom_axes` method of the
`~matplotlib.axes.Axes` class. Arguments for this method are backwards
compatible with the `~matplotlib.axes.Axes.inset_axes` method.

.. plot::
:include-source: true

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)
fig = plt.figure()
ax = fig.gca()
ax.plot([i for i in range(10)], "r-o")
ax.text(3, 2.5, "Hello World!", ha="center")
ax.imshow(np.random.rand(30, 30), origin="lower", cmap="Blues", alpha=0.5)
axins = ax.inset_zoom_axes([0.5, 0.5, 0.48, 0.48])
axins.set_xlim(1, 5)
axins.set_ylim(1, 5)
ax.indicate_inset_zoom(axins, edgecolor="black")
plt.show()
7 changes: 3 additions & 4 deletions examples/subplots_axes_and_figures/zoom_inset_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ def get_demo_image():
ny, nx = Z.shape
Z2[30:30+ny, 30:30+nx] = Z

ax.imshow(Z2, extent=extent, origin="lower")
ax.imshow(Z2, extent=extent, interpolation='nearest', origin="lower")

# inset axes....
axins = ax.inset_axes([0.5, 0.5, 0.47, 0.47])
axins.imshow(Z2, extent=extent, origin="lower")
axins = ax.inset_zoom_axes([0.5, 0.5, 0.47, 0.47])
# sub region of the original image
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
Expand All @@ -47,6 +46,6 @@ def get_demo_image():
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.inset_axes`
# - `matplotlib.axes.Axes.inset_zoom_axes`
# - `matplotlib.axes.Axes.indicate_inset_zoom`
# - `matplotlib.axes.Axes.imshow`
53 changes: 53 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,59 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):

return inset_ax

def inset_zoom_axes(self, bounds, *, transform=None, zorder=5,
image_interpolation="nearest", **kwargs):
"""
Add a child inset Axes to this existing Axes, which automatically plots
artists contained within the parent Axes.

Parameters
----------
bounds : [x0, y0, width, height]
Lower-left corner of inset Axes, and its width and height.

transform : `.Transform`
Defaults to `ax.transAxes`, i.e. the units of *rect* are in
Axes-relative coordinates.

zorder : number
Defaults to 5 (same as `.Axes.legend`). Adjust higher or lower
to change whether it is above or below data plotted on the
parent Axes.

image_interpolation: string
Supported options are 'antialiased', 'nearest', 'bilinear',
'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite',
'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
'sinc', 'lanczos', or 'none'. The default value is 'nearest'. This
determines the interpolation used when attempting to render a
zoomed version of an image.

**kwargs
Other keyword arguments are passed on to the child `.Axes`.

Returns
-------
ax
The created `~.axes.Axes` instance.

Examples
--------
See `Axes.inset_axes` method for examples.
"""
if(transform is None):
transform = self.transAxes

inset_loc = _TransformedBoundsLocator(bounds, transform)
bb = inset_loc(self, None)

from ._zoom_axes import ViewAxes
axin = ViewAxes(self, bb.bounds, zorder, image_interpolation, **kwargs)
axin.set_axes_locator(inset_loc)
self.add_child_axes(axin)

return axin

@docstring.dedent_interpd
def indicate_inset(self, bounds, inset_ax=None, *, transform=None,
facecolor='none', edgecolor='0.5', alpha=0.5,
Expand Down
Loading