8000 Merge pull request #22608 from rcomer/inset-add_axes · matplotlib/matplotlib@9caa261 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9caa261

Browse files
authored
Merge pull request #22608 from rcomer/inset-add_axes
Axes.inset_axes: enable Axes subclass creation
2 parents 2f7e53d + 80e672e commit 9caa261

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Axes.inset_axes Flexibility
2+
---------------------------
3+
4+
`matplotlib.axes.Axes.inset_axes` now accepts the *projection*, *polar* and
5+
*axes_class* keyword arguments, so that subclasses of `matplotlib.axes.Axes` may
6+
be returned.

lib/matplotlib/axes/_axes.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,27 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
325325
Defaults to `ax.transAxes`, i.e. the units of *rect* are in
326326
Axes-relative coordinates.
327327
328+
projection : {None, 'aitoff', 'hammer', 'lambert', 'mollweide', \
329+
'polar', 'rectilinear', str}, optional
330+
The projection type of the inset `~.axes.Axes`. *str* is the name
331+
of a custom projection, see `~matplotlib.projections`. The default
332+
None results in a 'rectilinear' projection.
333+
334+
polar : bool, default: False
335+
If True, equivalent to projection='polar'.
336+
337+
axes_class : subclass type of `~.axes.Axes`, optional
338+
The `.axes.Axes` subclass that is instantiated. This parameter
339+
is incompatible with *projection* and *polar*. See
340+
:ref:`axisartist_users-guide-index` for examples.
341+
328342
zorder : number
329343
Defaults to 5 (same as `.Axes.legend`). Adjust higher or lower
330344
to change whether it is above or below data plotted on the
331345
parent Axes.
332346
333347
**kwargs
334-
Other keyword arguments are passed on to the child `.Axes`.
348+
Other keyword arguments are passed on to the inset Axes class.
335349
336350
Returns
337351
-------
@@ -357,7 +371,10 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
357371
# This puts the rectangle into figure-relative coordinates.
358372
inset_locator = _TransformedBoundsLocator(bounds, transform)
359373
bounds = inset_locator(self, None).bounds
360-
inset_ax = Axes(self.figure, bounds, zorder=zorder, **kwargs)
374+
projection_class, pkw = self.figure._process_projection_requirements(
375+
bounds, **kwargs)
376+
inset_ax = projection_class(self.figure, bounds, zorder=zorder, **pkw)
377+
361378
# this locator lets the axes move if in data coordinates.
362379
# it gets called in `ax.apply_aspect() (of all places)
363380
inset_ax.set_axes_locator(inset_locator)
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
import matplotlib.markers as mmarkers
2727
import matplotlib.patches as mpatches
2828
import matplotlib.path as mpath
29+
from matplotlib.projections.geo import HammerAxes
30+
from matplotlib.projections.polar import PolarAxes
2931
import matplotlib.pyplot as plt
3032
import matplotlib.text as mtext
3133
import matplotlib.ticker as mticker
3234
import matplotlib.transforms as mtransforms
35+
import mpl_toolkits.axisartist as AA
3336
from numpy.testing import (
3437
assert_allclose, assert_array_equal, assert_array_almost_equal)
3538
from matplotlib.testing.decorators import (
@@ -2545,8 +2548,6 @@ def get_next_color():
25452548

25462549
def test_as_mpl_axes_api():
25472550
# tests the _as_mpl_axes api
2548-
from matplotlib.projections.polar import PolarAxes
2549-
25502551
class Polar:
25512552
def __init__(self):
25522553
self.theta_offset = 0
@@ -6625,6 +6626,31 @@ def test_zoom_inset():
66256626
axin1.get_position().get_points(), xx, rtol=1e-4)
66266627

66276628

6629+
@image_comparison(['inset_polar.png'], remove_text=True, style='mpl20')
6630+
def test_inset_polar():
6631+
_, ax = plt.subplots()
6632+
axins = ax.inset_axes([0.5, 0.1, 0.45, 0.45], polar=True)
6633+
assert isinstance(axins, PolarAxes)
6634+
6635+
r = np.arange(0, 2, 0.01)
6636+
theta = 2 * np.pi * r
6637+
6638+
ax.plot(theta, r)
6639+
axins.plot(theta, r)
6640+
6641+
6642+
def test_inset_projection():
6643+
_, ax = plt.subplots()
6644+
axins = ax.inset_axes([0.2, 0.2, 0.3, 0.3], projection="hammer")
6645+
assert isinstance(axins, HammerAxes)
6646+
6647+
6648+
def test_inset_subclass():
6649+
_, ax = plt.subplots()
6650+
axins = ax.inset_axes([0.2, 0.2, 0.3, 0.3], axes_class=AA.Axes)
6651+
assert isinstance(axins, AA.Axes)
6652+
6653+
66286654
@pytest.mark.parametrize('x_inverted', [False, True])
66296655
@pytest.mark.parametrize('y_inverted', [False, True])
66306656
def test_indicate_inset_inverted(x_inverted, y_inverted):

0 commit comments

Comments
 (0)
0