Skip to content

Commit 9952877

Browse files
committed
Align tags in the margins of plot compositions
1 parent dce8e71 commit 9952877

File tree

5 files changed

+737
-291
lines changed

5 files changed

+737
-291
lines changed

plotnine/_mpl/layout_manager/_layout_items.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,10 @@ def set_plot_tag_position(tag: Text, spaces: LayoutSpaces):
830830

831831
def set_plot_tag_position_in_margin(tag: Text, spaces: LayoutSpaces):
832832
"""
833-
Place the tag in the margin around the plot
833+
Place the tag in an inner margin around the plot
834+
835+
The panel_margin remains outside the tag. For compositions, the
836+
tag is placed and within the tag_alignment space.
834837
"""
835838
position: TagPosition = spaces.plot.theme.getp("plot_tag_position")
836839
if not isinstance(position, str):
@@ -840,18 +843,34 @@ def set_plot_tag_position_in_margin(tag: Text, spaces: LayoutSpaces):
840843
)
841844

842845
tag.set_position(spaces.to_figure_space((0.5, 0.5)))
843-
if "top" in position:
844-
tag.set_y(spaces.t.y2("plot_tag"))
845-
tag.set_verticalalignment("top")
846-
if "bottom" in position:
847-
tag.set_y(spaces.b.y1("plot_tag"))
848-
tag.set_verticalalignment("bottom")
849-
if "left" in position:
850-
tag.set_x(spaces.l.x1("plot_tag"))
846+
ha = spaces.plot.theme.get_ha("plot_tag")
847+
va = spaces.plot.theme.get_va("plot_tag")
848+
if "left" in position: # left, topleft, bottomleft
849+
space = spaces.l.tag_alignment
850+
x = spaces.l.x1("plot_tag") - (1 - ha) * space
851+
tag.set_x(x)
851852
tag.set_horizontalalignment("left")
852-
if "right" in position:
853-
tag.set_x(spaces.r.x2("plot_tag"))
854-
tag.set_horizontalalignment("right")
853+
if "right" in position: # right, topright, bottomright
854+
space = spaces.r.tag_alignment
855+
x = spaces.r.x1("plot_tag") + ha * space
856+
tag.set_x(x)
857+
tag.set_horizontalalignment("left")
858+
if "bottom" in position: # bottom, bottomleft, bottomright
859+
space = spaces.b.tag_alignment
860+
y = spaces.b.y1("plot_tag") + (1 - va) * space
861+
tag.set_y(y)
862+
tag.set_verticalalignment("bottom")
863+
if "top" in position: # top, topleft, topright
864+
space = spaces.t.tag_alignment
865+
y = spaces.t.y1("plot_tag") + va * space
866+
tag.set_y(y)
867+
tag.set_verticalalignment("bottom")
868+
869+
justify = TextJustifier(spaces)
870+
if position in ("left", "right"):
871+
justify.vertically_along_plot(tag, va)
872+
elif position in ("top", "bottom"):
873+
justify.horizontally_across_plot(tag, ha)
855874

856875

857876
def _plot_tag_margin_adjustment(

0 commit comments

Comments
 (0)