Skip to content

Commit c93ee89

Browse files
committed
Use rel_position to calculate alignment positions in the layout
1 parent 5205636 commit c93ee89

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

plotnine/_mpl/layout_manager/_layout_items.py

+33-22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ..utils import (
1414
bbox_in_figure_space,
1515
get_transPanels,
16+
rel_position,
1617
tight_bbox_in_figure_space,
1718
)
1819

@@ -534,9 +535,9 @@ def horizontally_align_text(
534535
"center": 0.5,
535536
"right": 1.0,
536537
}
537-
f = lookup[ha]
538+
rel = lookup[ha]
538539
else:
539-
f = ha
540+
rel = ha
540541

541542
if how == "panel":
542543
left = spaces.l.left
@@ -546,7 +547,7 @@ def horizontally_align_text(
546547
right = spaces.r.plot_right
547548

548549
width = spaces.items.calc.width(text)
549-
x = left * (1 - f) + (right - width) * f
550+
x = rel_position(rel, width, left, right)
550551
text.set_x(x)
551552
text.set_horizontalalignment("left")
552553

@@ -571,9 +572,9 @@ def vertically_align_text(
571572
"center_baseline": 0.5,
572573
"bottom": 0.0,
573574
}
574-
f = lookup[va]
575+
rel = lookup[va]
575576
else:
576-
f = va
577+
rel = va
577578

578579
if how == "panel":
579580
top = spaces.t.top
@@ -583,7 +584,7 @@ def vertically_align_text(
583584
bottom = spaces.b.plot_bottom
584585

585586
height = spaces.items.calc.height(text)
586-
y = bottom * (1 - f) + (top - height) * f
587+
y = rel_position(rel, height, bottom, top)
587588
text.set_y(y)
588589
text.set_verticalalignment("bottom")
589590

@@ -619,32 +620,42 @@ def set_position(
619620
aob.set_bbox_to_anchor(anchor_point, transform) # type: ignore
620621

621622
if legends.right:
622-
j = legends.right.justification
623-
y = (
624-
params.bottom * (1 - j)
625-
+ (params.top - spaces.r._legend_height) * j
623+
y = rel_position(
624+
legends.right.justification,
625+
spaces.r._legend_height,
626+
params.bottom,
627+
params.top,
626628
)
627629
x = spaces.r.x2("legend")
628630
set_position(legends.right.box, (x, y), (1, 0))
629631

630632
if legends.left:
631-
j = legends.left.justification
632-
y = (
633-
params.bottom * (1 - j)
634-
+ (params.top - spaces.l._legend_height) * j
633+
y = rel_position(
634+
legends.left.justification,
635+
spaces.l._legend_height,
636+
params.bottom,
637+
params.top,
635638
)
636639
x = spaces.l.x1("legend")
637640
set_position(legends.left.box, (x, y), (0, 0))
638641

639642
if legends.top:
640-
j = legends.top.justification
641-
x = params.left * (1 - j) + (params.right - spaces.t._legend_width) * j
643+
x = rel_position(
644+
legends.top.justification,
645+
spaces.t._legend_width,
646+
params.left,
647+
params.right,
648+
)
642649
y = spaces.t.y2("legend")
643650
set_position(legends.top.box, (x, y), (0, 1))
644651

645652
if legends.bottom:
646-
j = legends.bottom.justification
647-
x = params.left * (1 - j) + (params.right - spaces.b._legend_width) * j
653+
x = rel_position(
654+
legends.bottom.justification,
655+
spaces.b._legend_width,
656+
params.left,
657+
params.right,
658+
)
648659
y = spaces.b.y1("legend")
649660
set_position(legends.bottom.box, (x, y), (0, 0))
650661

@@ -680,17 +691,17 @@ def set_plot_tag_position(tag: Text, spaces: LayoutSpaces):
680691
}
681692

682693
if isinstance(position, str):
683-
# Coordinates of the space to place the tag
694+
# Coordinates of the space in which to place the tag
684695
if location == "plot":
685696
(x1, y1), (x2, y2) = spaces.plot_area_coordinates
686697
else:
687698
(x1, y1), (x2, y2) = spaces.panel_area_coordinates
688699

689700
# Calculate the position when the tag has no margins
690-
fx, fy = lookup[position]
701+
rel_x, rel_y = lookup[position]
691702
width, height = spaces.items.calc.size(tag)
692-
x = x1 * (1 - fx) + (x2 - width) * fx
693-
y = y1 * (1 - fy) + (y2 - height) * fy
703+
x = rel_position(rel_x, width, x1, x2)
704+
y = rel_position(rel_y, height, y1, y2)
694705

695706
# Adjust the position to account for the margins
696707
# When the units for the margin are in the figure coordinates,

plotnine/_mpl/utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,21 @@ def get_transPanels(fig: Figure) -> Transform:
7676
dx, dy = params.left * W, params.bottom * H
7777
transFiguretoPanels = Affine2D().scale(sx, sy).translate(dx, dy)
7878
return fig.transFigure + transFiguretoPanels
79+
80+
81+
def rel_position(rel: float, length: float, low: float, high: float) -> float:
82+
"""
83+
Relatively position an object of a given length between two position
84+
85+
Parameters
86+
----------
87+
rel:
88+
Relative position of the object between the limits.
89+
length:
90+
Length of the object
91+
low:
92+
Lower limit position
93+
high:
94+
Upper limit position
95+
"""
96+
return low * (1 - rel) + (high - length) * rel

0 commit comments

Comments
 (0)