Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cebac4f

Browse files
committed
add zoom_inset_rectangle
1 parent 24c4f7f commit cebac4f

File tree

2 files changed

+90
-22
lines changed

2 files changed

+90
-22
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def _remove_legend(self, legend):
405405

406406
def inset_axes(self, rect, transform=None, **kwargs):
407407
"""
408-
Add a child axes to the existing axes.
408+
Add a child inset axes to this existing axes.
409409
410410
Parameters
411411
----------
@@ -417,6 +417,10 @@ def inset_axes(self, rect, transform=None, **kwargs):
417417
Defaults to `ax.transAxes`, i.e. the units of *rect* are in
418418
axes-relative coordinates.
419419
420+
zorder : number
421+
Defaults to 5 (same as `.Axes.legend`). Adjust higher or lower
422+
to change whether it is above or below data plotted on the axes.
423+
420424
Other Parameters
421425
----------------
422426
@@ -447,64 +451,91 @@ def inset_axes(self, rect, transform=None, **kwargs):
447451
bb = _inset_locator(rect, transform, self)(None, None)
448452
# if not set, set to the same zorder as legend
449453
zorder = kwargs.pop('zorder', 5)
450-
axins = Axes(self.figure, bb.bounds, zorder=zorder,
454+
inset_ax = Axes(self.figure, bb.bounds, zorder=zorder,
451455
label=label, **kwargs)
452456

453457
# this locator lets the axes move if in data coordinates.
454458
# it gets called in `ax.apply_aspect() (of all places)
455459
axes_locator = _inset_locator(rect, transform, self)
456-
axins.set_axes_locator(axes_locator)
460+
inset_ax.set_axes_locator(axes_locator)
457461

458-
self.add_child_axes(axins)
462+
self.add_child_axes(inset_ax)
459463

460-
return axins
464+
return inset_ax
461465

462-
def inset_indicator(self, rect, axins=None, transform=None,
466+
def inset_rectangle(self, rect, inset_ax=None, transform=None,
463467
facecolor='none', edgecolor='0.5', alpha=0.5,
464468
zorder=4.99, **kwargs):
465469
"""
466-
Add an inset indicator to the axes. This is a box on the plot that
467-
can optionally have lines joining it to an inset axes *axins*.
470+
Add an inset indicator to the axes. This is a rectangle on the plot
471+
at the position indicated by *rect* that optionally has lines that
472+
connect the rectangle to an inset axes (`.Axes.inset_axes`).
468473
469474
Parameters
470475
----------
471476
472477
rect : [x0, y0, width, height]
473-
Lower-left corner of area to be marked, and its width and height.
478+
Lower-left corner of rectangle to be marked, and its width
479+
and height.
474480
475-
axins : `Axes`
476-
An optional axes to join this indicator to. Two lines are drawn
477-
connecting the indicator box to the inset axes on corners chosen
478-
so as to not overlap with the indicator box.
481+
inset_ax : `.Axes`
482+
An optional inset axes to draw connecting lines to. Two lines are
483+
drawn connecting the indicator box to the inset axes on corners
484+
chosen so as to not overlap with the indicator box.
479485
480486
transform : `.Transform`
481-
Defaults to `ax.transAxes`, i.e. the units of *rect* are in
482-
axes-relative coordinates.
487+
Transform for the rectangle co-ordinates. Defaults to
488+
`ax.transAxes`, i.e. the units of *rect* are in axes-relative
489+
coordinates.
490+
491+
facecolor : Matplotlib color
492+
Facecolor of the rectangle (default 'none')
493+
494+
edgecolor : Matplotlib color
495+
Color of the rectangle and color of the connecting lines. Default
496+
is '0.5'
497+
498+
alpha : number
499+
Transparency of the rectangle and connector lines. Default 0.5
500+
501+
zorder : number
502+
drawing order of the rectangle and connector lines. Default 4.99
503+
(just below the default level of inset axes)
483504
484505
Other Parameters
485506
----------------
486507
487-
kwargs are passed on to the `axes.Axes` child axes.
508+
Other *kwargs* are passed on to the rectangle patch.
488509
489510
Returns
490511
-------
491512
492-
??
513+
rectangle_patch, connector_lines :
514+
`.Patches.Rectagle`, (four-tupple `.Patches.ConnectionPatch`) one
515+
for each of four connector lines. Two are set with visibility to
516+
*False*, but the user can set the visibility to True if the
517+
automatic choice is not deemed correct.
493518
494519
"""
520+
521+
# to make the axes connectors work, we need to apply the aspect to
522+
# the parent axes.
523+
self.apply_aspect()
524+
495525
if transform is None:
496526
transform = self.transData
497527
label = kwargs.pop('label', 'inset_indicator')
528+
498529
xy = (rect[0], rect[1])
499530
rectpatch = mpatches.Rectangle(xy, rect[2], rect[3],
500531
facecolor=facecolor, edgecolor=edgecolor, alpha=alpha,
501532
zorder=zorder, label=label, transform=transform, **kwargs)
502533
self.add_patch(rectpatch)
503534

504-
if not axins is None:
535+
if not inset_ax is None:
505536
# want to connect the indicator to the rect....
506537

507-
pos = axins.get_position() # this is in fig-fraction.
538+
pos = inset_ax.get_position() # this is in fig-fraction.
508539
coordsA='axes fraction'
509540
connects = []
510541
xr = [rect[0], rect[0]+rect[2]]
@@ -515,11 +546,12 @@ def inset_indicator(self, rect, axins=None, transform=None,
515546
xyB = (xr[xc], yr[yc])
516547
connects += [mpatches.ConnectionPatch(xyA, xyB,
517548
'axes fraction', 'data',
518-
axesA=axins, axesB=self, arrowstyle="-",
549+
axesA=inset_ax, axesB=self, arrowstyle="-",
519550
zorder=zorder, edgecolor=edgecolor, alpha=alpha)]
520551
self.add_patch(connects[-1])
521552
# decide which two of the lines to keep visible....
522-
bboxins = axins.get_position().transformed(self.figure.transFigure)
553+
pos = inset_ax.get_position()
554+
bboxins = pos.transformed(self.figure.transFigure)
523555
rectbbox = mtransforms.Bbox.from_bounds(rect[0], rect[1],
524556
rect[2], rect[3]).transformed(transform)
525557
if rectbbox.x0 < bboxins.x0:
@@ -533,10 +565,44 @@ def inset_indicator(self, rect, axins=None, transform=None,
533565
connects[1].set_visible(False)
534566
connects[2].set_visible(False)
535567

536-
return rectpatch, connects
568+
return rectpatch, connects
569+
570+
def zoom_inset_rectangle(self, inset_ax, **kwargs):
571+
"""
572+
Add an inset indicator rectangle to the axes based on the axis
573+
limits for an *inset_ax* and draw connectors between *inset_ax*
574+
and the rectangle.
575+
576+
Parameters
577+
----------
578+
579+
inset_ax : `.Axes`
580+
Inset axes to draw connecting lines to. Two lines are
581+
drawn connecting the indicator box to the inset axes on corners
582+
chosen so as to not overlap with the indicator box.
583+
584+
Other Parameters
585+
----------------
537586
587+
Other *kwargs* are passed on to `.Axes.inset_rectangle`
588+
589+
Returns
590+
-------
591+
592+
rectangle_patch, connector_lines :
593+
`.Patches.Rectagle`, (four-tupple `.Patches.ConnectionPatch`) one
594+
for each of four connector lines. Two are set with visibility to
595+
*False*, but the user can set the visibility to True if the
596+
automatic choice is not deemed correct.
597+
598+
"""
538599

600+
xlim = inset_ax.get_xlim()
601+
ylim = inset_ax.get_ylim()
602+
rect = [xlim[0], ylim[0], xlim[1] - xlim[0], ylim[1] - ylim[0]]
603+
rectpatch, connects = self.inset_rectangle(rect, inset_ax, **kwargs)
539604

605+
return rectpatch, connects
540606

541607
def text(self, x, y, s, fontdict=None, withdash=False, **kwargs):
542608
"""

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,6 +4140,8 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
41404140
bb.append(child.get_window_extent(renderer))
41414141
elif isinstance(child, Legend) and child.get_visible():
41424142
bb.append(child._legend_box.get_window_extent(renderer))
4143+
elif isinstance(child, _AxesBase) and child.get_visible():
4144+
bb.append(child.get_tightbbox(renderer))
41434145

41444146
_bbox = mtransforms.Bbox.union(
41454147
[b for b in bb if b.width != 0 or b.height != 0])

0 commit comments

Comments
 (0)