ENH add an inset_axes to the axes class by jklymak · Pull Request #11026 · matplotlib/matplotlib · GitHub
Skip to content
Merged
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 .flake8
3 changes: 3 additions & 0 deletions doc/api/axes_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ Text and Annotations
Axes.text
Axes.table
Axes.arrow
Axes.inset_axes
Axes.indicate_inset
Axes.indicate_inset_zoom


Fields
Expand Down
6 changes: 3 additions & 3 deletions examples/axes_grid1/inset_locator_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"""

###############################################################################
# The `.inset_locator`'s `~.inset_axes` allows to easily place insets in the
# corners of the axes by specifying a width and height and optionally
# a location (loc) which accepts locations as codes, similar to
# The `.inset_locator`'s `~.axes_grid1.inset_axes` allows to easily place
# insets in the corners of the axes by specifying a width and height and
# optionally a location (loc) which accepts locations as codes, similar to
# `~matplotlib.axes.Axes.legend`.
# By default, the inset is offset by some points from the axes - this is
# controlled via the `borderpad` parameter.
Expand Down
60 changes: 60 additions & 0 deletions examples/subplots_axes_and_figures/zoom_inset_axes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
======================
Zoom region inset axes
======================

Example of an inset axes and a rectangle showing where the zoom is located.

"""

import matplotlib.pyplot as plt
import numpy as np


def get_demo_image():
from matplotlib.cbook import get_sample_data
import numpy as np
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
# z is a numpy array of 15x15
return z, (-3, 4, -4, 3)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just stumbled over this. To make it clear what those magic numbers are use

extent = (-3, 4, -4, 3)
return z, extent

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed, though I have a sneaky suspicion that I had it that way, and someone suggested I shorten it (ahem, @anntzer 😉)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For once I don't think it's my fault :) (?)


fig, ax = plt.subplots(figsize=[5, 4])

# make data
Z, extent = get_demo_image()
Z2 = np.zeros([150, 150], dtype="d")
ny, nx = Z.shape
Z2[30:30 + ny, 30:30 + nx] = Z

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, interpolation="nearest",
origin="lower")
# sub region of the original image
x1, x2, y1, y2 = -1.5, -0.9, -2.5, -1.9
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
axins.set_xticklabels('')
axins.set_yticklabels('')

ax.indicate_inset_zoom(axins)

plt.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions and methods is shown in this example:

import matplotlib
matplotlib.axes.Axes.inset_axes
matplotlib.axes.Axes.indicate_inset_zoom
matplotlib.axes.Axes.imshow
249 changes: 249 additions & 0 deletions lib/matplotlib/axes/_axes.py
Loading