Skip to content

Commit 32d3386

Browse files
committed
Minor internal improvements
1 parent ce12f6f commit 32d3386

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

proplot/axes/base.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,21 @@
646646
docstring._snippet_manager['axes.legend_kwargs'] = _legend_kwargs_docstring
647647

648648

649+
def _align_bbox(align, length):
650+
"""
651+
Return a simple alignment bounding box for intersection calculations.
652+
"""
653+
if align in ('left', 'bottom'):
654+
bounds = [[0, 0], [length, 0]]
655+
elif align in ('top', 'right'):
656+
bounds = [[1 - length, 0], [1, 0]]
657+
elif align == 'center':
658+
bounds = [[0.5 * (1 - length), 0], [0.5 * (1 + length), 0]]
659+
else:
660+
raise ValueError(f'Invalid align {align!r}.')
661+
return mtransforms.Bbox(bounds)
662+
663+
649664
class _TransformedBoundsLocator:
650665
"""
651666
Axes locator for `~Axes.inset_axes` and other axes.
@@ -856,15 +871,6 @@ def _add_guide_panel(self, loc='fill', align='center', length=0, **kwargs):
856871
"""
857872
Add a panel to be filled by an "outer" colorbar or legend.
858873
"""
859-
# Helper function: Return bounds inferred from given align setting.
860-
def _align_bbox(align, length):
861-
if align in ('left', 'bottom'):
862-
bounds = [[0, 0], [length, 0]]
863-
elif align in ('top', 'right'):
864-
bounds = [[1 - length, 0], [1, 0]]
865-
else:
866-
bounds = [[0.5 * (1 - length), 0], [0.5 * (1 + length), 0]]
867-
return mtransforms.Bbox(bounds)
868874
# NOTE: For colorbars we include 'length' when determining whether to allocate
869875
# new panel but for legend just test whether that 'align' position was filled.
870876
# WARNING: Hide content but 1) do not use ax.set_visible(False) so that
@@ -878,7 +884,7 @@ def _align_bbox(align, length):
878884
for pax in self._panel_dict[loc]:
879885
if not pax._panel_hidden or align in pax._panel_align:
880886
continue
881-
if not any(bbox.overlaps(_align_bbox(a, l)) for a, l in pax._panel_align.items()): # noqa: E501
887+
if not any(bbox.overlaps(b) for b in pax._panel_align.values()):
882888
ax = pax
883889
break
884890
if ax is None:
@@ -891,7 +897,7 @@ def _align_bbox(align, length):
891897
ax.yaxis.set_visible(False)
892898
ax.patch.set_facecolor('none')
893899
ax._panel_hidden = True
894-
ax._panel_align[align] = length
900+
ax._panel_align[align] = bbox
895901
return ax
896902

897903
def _add_inset_axes(

proplot/gridspec.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,23 +468,21 @@ def _parse_panel_arg(self, side, arg):
468468
array = self._fpanels[side]
469469
nacross = self._ncols_total if side in ('left', 'right') else self._nrows_total # noqa: E501
470470
npanels, nalong = array.shape
471-
arg = _not_none(arg, (1, nalong))
472-
arg = np.atleast_1d(arg)
471+
arg = np.atleast_1d(_not_none(arg, (1, nalong)))
473472
if arg.size not in (1, 2):
474473
raise ValueError(f'Invalid span={arg!r}. Must be scalar or 2-tuple of coordinates.') # noqa: E501
475474
if any(s < 1 or s > nalong for s in arg):
476475
raise ValueError(f'Invalid span={arg!r}. Coordinates must satisfy 1 <= c <= {nalong}.') # noqa: E501
477476
start, stop = arg[0] - 1, arg[-1] # non-inclusive starting at zero
478477
iratio = -1 if side in ('left', 'top') else nacross # default values
479478
for i in range(npanels): # possibly use existing panel slot
480-
if any(array[i, start:stop]): # filled
481-
continue
482-
array[i, start:stop] = True
483-
if side in ('left', 'top'): # descending moves us closer to 0
484-
iratio = npanels - 1 - i # index in ratios array
485-
else: # descending array moves us closer to nacross - 1
486-
iratio = nacross - (npanels - i) # index in ratios array
487-
break
479+
if not any(array[i, start:stop]):
480+
array[i, start:stop] = True
481+
if side in ('left', 'top'): # descending moves us closer to 0
482+
iratio = npanels - 1 - i # index in ratios array
483+
else: # descending array moves us closer to nacross - 1
484+
iratio = nacross - (npanels - i) # index in ratios array
485+
break
488486
if iratio == -1 or iratio == nacross: # no slots so we must add to array
489487
iarray = np.zeros((1, nalong), dtype=bool)
490488
iarray[0, start:stop] = True

0 commit comments

Comments
 (0)