Skip to content

Axes.inset_axes: enable Axes subclass creation #22608

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

Merged
merged 1 commit into from
Apr 7, 2022
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
6 changes: 6 additions & 0 deletions doc/users/next_whats_new/inset_axes_improvements.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Axes.inset_axes Flexibility
---------------------------

`matplotlib.axes.Axes.inset_axes` now accepts the *projection*, *polar* and
*axes_class* keyword arguments, so that subclasses of `matplotlib.axes.Axes` may
be returned.
21 changes: 19 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,27 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
Defaults to `ax.transAxes`, i.e. the units of *rect* are in
Axes-relative coordinates.

projection : {None, 'aitoff', 'hammer', 'lambert', 'mollweide', \
'polar', 'rectilinear', str}, optional
The projection type of the inset `~.axes.Axes`. *str* is the name
of a custom projection, see `~matplotlib.projections`. The default
None results in a 'rectilinear' projection.

polar : bool, default: False
If True, equivalent to projection='polar'.

axes_class : subclass type of `~.axes.Axes`, optional
The `.axes.Axes` subclass that is instantiated. This parameter
is incompatible with *projection* and *polar*. See
:ref:`axisartist_users-guide-index` for examples.

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.

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

Returns
-------
Expand All @@ -358,7 +372,10 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
# This puts the rectangle into figure-relative coordinates.
inset_locator = _TransformedBoundsLocator(bounds, transform)
bounds = inset_locator(self, None).bounds
inset_ax = Axes(self.figure, bounds, zorder=zorder, **kwargs)
projection_class, pkw = self.figure._process_projection_requirements(
bounds, **kwargs)
inset_ax = projection_class(self.figure, bounds, zorder=zorder, **pkw)

# this locator lets the axes move if in data coordinates.
# it gets called in `ax.apply_aspect() (of all places)
inset_ax.set_axes_locator(inset_locator)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 28 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
import matplotlib.markers as mmarkers
import matplotlib.patches as mpatches
import matplotlib.path as mpath
from matplotlib.projections.geo import HammerAxes
from matplotlib.projections.polar import PolarAxes
import matplotlib.pyplot as plt
import matplotlib.text as mtext
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
import mpl_toolkits.axisartist as AA
from numpy.testing import (
assert_allclose, assert_array_equal, assert_array_almost_equal)
from matplotlib.testing.decorators import (
Expand Down Expand Up @@ -2543,8 +2546,6 @@ def get_next_color():

def test_as_mpl_axes_api():
# tests the _as_mpl_axes api
from matplotlib.projections.polar import PolarAxes

class Polar:
def __init__(self):
self.theta_offset = 0
Expand Down Expand Up @@ -6614,6 +6615,31 @@ def test_zoom_inset():
axin1.get_position().get_points(), xx, rtol=1e-4)


@image_comparison(['inset_polar.png'], remove_text=True, style='mpl20')
def test_inset_polar():
_, ax = plt.subplots()
axins = ax.inset_axes([0.5, 0.1, 0.45, 0.45], polar=True)
assert isinstance(axins, PolarAxes)

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax.plot(theta, r)
axins.plot(theta, r)


def test_inset_projection():
_, ax = plt.subplots()
axins = ax.inset_axes([0.2, 0.2, 0.3, 0.3], projection="hammer")
assert isinstance(axins, HammerAxes)


def test_inset_subclass():
_, ax = plt.subplots()
axins = ax.inset_axes([0.2, 0.2, 0.3, 0.3], axes_class=AA.Axes)
assert isinstance(axins, AA.Axes)


@pytest.mark.parametrize('x_inverted', [False, True])
@pytest.mark.parametrize('y_inverted', [False, True])
def test_indicate_inset_inverted(x_inverted, y_inverted):
Expand Down