Skip to content

Commit 80e672e

Browse files
committed
enable Axes subclass creation by Axes.inset_axes
1 parent 808dbe4 commit 80e672e

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
@@ -326,13 +326,27 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
326326
Defaults to `ax.transAxes`, i.e. the units of *rect* are in
327327
Axes-relative coordinates.
328328
329+
projection : {None, 'aitoff', 'hammer', 'lambert', 'mollweide', \
330+
'polar', 'rectilinear', str}, optional
331+
The projection type of the inset `~.axes.Axes`. *str* is the name
332+
of a custom projection, see `~matplotlib.projections`. The default
333+
None results in a 'rectilinear' projection.
334+
335+
polar : bool, default: False
336+
If True, equivalent to projection='polar'.
337+
338+
axes_class : subclass type of `~.axes.Axes`, optional
339+
The `.axes.Axes` subclass that is instantiated. This parameter
340+
is incompatible with *projection* and *polar*. See
341+
:ref:`axisartist_users-guide-index` for examples.
342+
329343
zorder : number
330344
Defaults to 5 (same as `.Axes.legend`). Adjust higher or lower
331345
to change whether it is above or below data plotted on the
332346
parent Axes.
333347
334348
**kwargs
335-
Other keyword arguments are passed on to the child `.Axes`.
349+
Other keyword arguments are passed on to the inset Axes class.
336350
337351
Returns
338352
-------
@@ -358,7 +372,10 @@ def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs):
358372
# This puts the rectangle into figure-relative coordinates.
359373
inset_locator = _TransformedBoundsLocator(bounds, transform)
360374
bounds = inset_locator(self, None).bounds
361-
inset_ax = Axes(self.figure, bounds, zorder=zorder, **kwargs)
375+
projection_class, pkw = self.figure._process_projection_requirements(
376+
bounds, **kwargs)
377+
inset_ax = projection_class(self.figure, bounds, zorder=zorder, **pkw)
378+
362379
# this locator lets the axes move if in data coordinates.
363380
# it gets called in `ax.apply_aspect() (of all places)
364381
inset_ax.set_axes_locator(inset_locator)

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 (
@@ -2543,8 +2546,6 @@ def get_next_color():
25432546

25442547
def test_as_mpl_axes_api():
25452548
# tests the _as_mpl_axes api
2546-
from matplotlib.projections.polar import PolarAxes
2547-
25482549
class Polar:
25492550
def __init__(self):
25502551
self.theta_offset = 0
@@ -6614,6 +6615,31 @@ def test_zoom_inset():
66146615
axin1.get_position().get_points(), xx, rtol=1e-4)
66156616

66166617

6618+
@image_comparison(['inset_polar.png'], remove_text=True, style='mpl20')
6619+
def test_inset_polar():
6620+
_, ax = plt.subplots()
6621+
axins = ax.inset_axes([0.5, 0.1, 0.45, 0.45], polar=True)
6622+
assert isinstance(axins, PolarAxes)
6623+
6624+
r = np.arange(0, 2, 0.01)
6625+
theta = 2 * np.pi * r
6626+
6627+
ax.plot(theta, r)
6628+
axins.plot(theta, r)
6629+
6630+
6631+
def test_inset_projection():
6632+
_, ax = plt.subplots()
6633+
axins = ax.inset_axes([0.2, 0.2, 0.3, 0.3], projection="hammer")
6634+
assert isinstance(axins, HammerAxes)
6635+
6636+
6637+
def test_inset_subclass():
6638+
_, ax = plt.subplots()
6639+
axins = ax.inset_axes([0.2, 0.2, 0.3, 0.3], axes_class=AA.Axes)
6640+
assert isinstance(axins, AA.Axes)
6641+
6642+
66176643
@pytest.mark.parametrize('x_inverted', [False, True])
66186644
@pytest.mark.parametrize('y_inverted', [False, True])
66196645
def test_indicate_inset_inverted(x_inverted, y_inverted):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy