Skip to content

Commit 4dca718

Browse files
committed
Use one TextJustifier class
1 parent a1c8e77 commit 4dca718

3 files changed

Lines changed: 64 additions & 45 deletions

File tree

plotnine/_mpl/layout_manager/_composition_layout_items.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from plotnine._mpl.utils import (
99
ArtistGeometry,
10-
JustifyBoundaries,
1110
TextJustifier,
1211
)
1312

@@ -64,7 +63,17 @@ def _move_artists(self, spaces: CompositionSideSpaces):
6463
plot_title_position = theme.getp("plot_title_position", "panel")
6564
plot_caption_position = theme.getp("plot_caption_position", "panel")
6665
plot_footer_position = theme.getp("plot_footer_position", "plot")
67-
justify = CompositionTextJustifier(spaces)
66+
justify = TextJustifier.from_boundaries(
67+
spaces.cmp.figure,
68+
plot_left=spaces.plot_left,
69+
plot_right=spaces.plot_right,
70+
plot_bottom=spaces.plot_bottom,
71+
plot_top=spaces.plot_top,
72+
panel_left=spaces.panel_left,
73+
panel_right=spaces.panel_right,
74+
panel_bottom=spaces.panel_bottom,
75+
panel_top=spaces.panel_top,
76+
)
6877

6978
if self.plot_title:
7079
ha = theme.getp(("plot_title", "ha"))
@@ -120,22 +129,3 @@ def _resize_plot_footer_line(self, spaces: CompositionSideSpaces):
120129
y1 = y2 = spaces.b.offset + spaces.b.footer_height
121130
self.plot_footer_line.set_xdata([x1, x2])
122131
self.plot_footer_line.set_ydata([y1, y2])
123-
124-
125-
class CompositionTextJustifier(TextJustifier):
126-
"""
127-
Justify Text about a composition or it's panels
128-
"""
129-
130-
def __init__(self, spaces: CompositionSideSpaces):
131-
boundaries = JustifyBoundaries(
132-
plot_left=spaces.plot_left,
133-
plot_right=spaces.plot_right,
134-
plot_bottom=spaces.plot_bottom,
135-
plot_top=spaces.plot_top,
136-
panel_left=spaces.panel_left,
137-
panel_right=spaces.panel_right,
138-
panel_bottom=spaces.panel_bottom,
139-
panel_top=spaces.panel_top,
140-
)
141-
super().__init__(spaces.cmp.figure, boundaries)

plotnine/_mpl/layout_manager/_plot_layout_items.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from ..utils import (
1212
ArtistGeometry,
13-
JustifyBoundaries,
1413
TextJustifier,
1514
get_subplotspecs,
1615
rel_position,
@@ -354,7 +353,17 @@ def _move_artists(self, spaces: PlotSideSpaces):
354353
plot_title_position = theme.getp("plot_title_position", "panel")
355354
plot_caption_position = theme.getp("plot_caption_position", "panel")
356355
plot_footer_position = theme.getp("plot_footer_position", "plot")
357-
justify = PlotTextJustifier(spaces)
356+
justify = TextJustifier.from_boundaries(
357+
spaces.plot.figure,
358+
plot_left=spaces.l.plot_left,
359+
plot_right=spaces.r.plot_right,
360+
plot_bottom=spaces.b.plot_bottom,
361+
plot_top=spaces.t.plot_top,
362+
panel_left=spaces.l.panel_left,
363+
panel_right=spaces.r.panel_right,
364+
panel_bottom=spaces.b.panel_bottom,
365+
panel_top=spaces.t.panel_top,
366+
)
358367

359368
if self.plot_tag:
360369
set_plot_tag_position(self.plot_tag, spaces)
@@ -407,7 +416,7 @@ def _move_artists(self, spaces: PlotSideSpaces):
407416
self._strip_text_x_background_equal_heights()
408417
self._strip_text_y_background_equal_widths()
409418

410-
def _adjust_axis_text_x(self, justify: PlotTextJustifier):
419+
def _adjust_axis_text_x(self, justify: TextJustifier):
411420
"""
412421
Adjust x-axis text, justifying vertically as necessary
413422
"""
@@ -438,7 +447,7 @@ def to_vertical_axis_dimensions(value: float, ax: Axes) -> float:
438447
text, va, -axis_text_row_height, 0, height=height
439448
)
440449

441-
def _adjust_axis_text_y(self, justify: PlotTextJustifier):
450+
def _adjust_axis_text_y(self, justify: TextJustifier):
442451
"""
443452
Adjust x-axis text, justifying horizontally as necessary
444453
"""
@@ -556,25 +565,6 @@ def _text_is_visible(text: Text) -> bool:
556565
return text.get_visible() and text._text # type: ignore
557566

558567

559-
class PlotTextJustifier(TextJustifier):
560-
"""
561-
Justify Text about a plot or it's panels
562-
"""
563-
564-
def __init__(self, spaces: PlotSideSpaces):
565-
boundaries = JustifyBoundaries(
566-
plot_left=spaces.l.plot_left,
567-
plot_right=spaces.r.plot_right,
568-
plot_bottom=spaces.b.plot_bottom,
569-
plot_top=spaces.t.plot_top,
570-
panel_left=spaces.l.panel_left,
571-
panel_right=spaces.r.panel_right,
572-
panel_bottom=spaces.b.panel_bottom,
573-
panel_top=spaces.t.panel_top,
574-
)
575-
super().__init__(spaces.plot.figure, boundaries)
576-
577-
578568
def set_legends_position(legends: legend_artists, spaces: PlotSideSpaces):
579569
"""
580570
Place legend on the figure and justify is a required
@@ -752,7 +742,17 @@ def set_plot_tag_position_in_margin(tag: Text, spaces: PlotSideSpaces):
752742
tag.set_y(y)
753743
tag.set_verticalalignment("bottom")
754744

755-
justify = PlotTextJustifier(spaces)
745+
justify = TextJustifier.from_boundaries(
746+
spaces.plot.figure,
747+
plot_left=spaces.l.plot_left,
748+
plot_right=spaces.r.plot_right,
749+
plot_bottom=spaces.b.plot_bottom,
750+
plot_top=spaces.t.plot_top,
751+
panel_left=spaces.l.panel_left,
752+
panel_right=spaces.r.panel_right,
753+
panel_bottom=spaces.b.panel_bottom,
754+
panel_top=spaces.t.panel_top,
755+
)
756756
if position in ("left", "right"):
757757
justify.vertically_along_plot(tag, va)
758758
elif position in ("top", "bottom"):

plotnine/_mpl/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,35 @@ def __init__(self, figure: Figure, boundaries: JustifyBoundaries):
304304
self.geometry = ArtistGeometry(figure)
305305
self.boundaries = boundaries
306306

307+
@classmethod
308+
def from_boundaries(
309+
cls,
310+
figure: Figure,
311+
*,
312+
plot_left: float,
313+
plot_right: float,
314+
plot_bottom: float,
315+
plot_top: float,
316+
panel_left: float,
317+
panel_right: float,
318+
panel_bottom: float,
319+
panel_top: float,
320+
) -> TextJustifier:
321+
"""
322+
Create a TextJustifier from boundary coordinates
323+
"""
324+
boundaries = JustifyBoundaries(
325+
plot_left=plot_left,
326+
plot_right=plot_right,
327+
plot_bottom=plot_bottom,
328+
plot_top=plot_top,
329+
panel_left=panel_left,
330+
panel_right=panel_right,
331+
panel_bottom=panel_bottom,
332+
panel_top=panel_top,
333+
)
334+
return cls(figure, boundaries)
335+
307336
def horizontally(
308337
self,
309338
text: Text,

0 commit comments

Comments
 (0)