Skip to content

Commit a5dc95a

Browse files
committed
Use helper functions to adjust the plot footer
1 parent 4dca718 commit a5dc95a

3 files changed

Lines changed: 65 additions & 54 deletions

File tree

plotnine/_mpl/layout_manager/_composition_layout_items.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from plotnine._mpl.utils import (
99
ArtistGeometry,
1010
TextJustifier,
11+
resize_footer_background,
12+
resize_footer_line,
1113
)
1214

1315
if TYPE_CHECKING:
@@ -102,30 +104,18 @@ def _move_artists(self, spaces: CompositionSideSpaces):
102104
justify.horizontally_about(
103105
self.plot_footer, ha, plot_footer_position
104106
)
105-
self._resize_plot_footer_background(spaces)
106-
self._resize_plot_footer_line(spaces)
107-
108-
def _resize_plot_footer_background(self, spaces: CompositionSideSpaces):
109-
"""
110-
Resize the plot footer to the size of the footer
111-
"""
112-
if not self.plot_footer_background:
113-
return
114-
115-
self.plot_footer_background.set_x(spaces.l.offset)
116-
self.plot_footer_background.set_y(spaces.b.offset)
117-
self.plot_footer_background.set_height(spaces.b.footer_height)
118-
self.plot_footer_background.set_width(spaces.plot_width)
119-
120-
def _resize_plot_footer_line(self, spaces: CompositionSideSpaces):
121-
"""
122-
Resize the footer line to be a border above the footer
123-
"""
124-
if not self.plot_footer_line:
125-
return
126-
127-
x1 = spaces.l.offset
128-
x2 = x1 + spaces.plot_width
129-
y1 = y2 = spaces.b.offset + spaces.b.footer_height
130-
self.plot_footer_line.set_xdata([x1, x2])
131-
self.plot_footer_line.set_ydata([y1, y2])
107+
if self.plot_footer_background:
108+
resize_footer_background(
109+
self.plot_footer_background,
110+
x=spaces.l.offset,
111+
y=spaces.b.offset,
112+
height=spaces.b.footer_height,
113+
width=spaces.plot_width,
114+
)
115+
if self.plot_footer_line:
116+
resize_footer_line(
117+
self.plot_footer_line,
118+
x=spaces.l.offset,
119+
width=spaces.plot_width,
120+
y=spaces.b.offset + spaces.b.footer_height,
121+
)

plotnine/_mpl/layout_manager/_plot_layout_items.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
TextJustifier,
1414
get_subplotspecs,
1515
rel_position,
16+
resize_footer_background,
17+
resize_footer_line,
1618
)
1719

1820
if TYPE_CHECKING:
@@ -395,8 +397,21 @@ def _move_artists(self, spaces: PlotSideSpaces):
395397
justify.horizontally_about(
396398
self.plot_footer, ha, plot_footer_position
397399
)
398-
self._resize_plot_footer_background(spaces)
399-
self._resize_plot_footer_line(spaces)
400+
if self.plot_footer_background:
401+
resize_footer_background(
402+
self.plot_footer_background,
403+
x=spaces.l.offset,
404+
y=spaces.b.offset,
405+
height=spaces.b.footer_height,
406+
width=spaces.plot_width,
407+
)
408+
if self.plot_footer_line:
409+
resize_footer_line(
410+
self.plot_footer_line,
411+
x=spaces.l.offset,
412+
width=spaces.plot_width,
413+
y=spaces.b.offset + spaces.b.footer_height,
414+
)
400415

401416
if self.axis_title_x:
402417
ha = theme.getp(("axis_title_x", "ha"), "center")
@@ -532,31 +547,6 @@ def _strip_text_y_background_equal_widths(self):
532547
for text, scale in zip(self.strip_text_y, relative_widths):
533548
text.patch.expand = scale
534549

535-
def _resize_plot_footer_background(self, spaces: PlotSideSpaces):
536-
"""
537-
Resize the plot footer to the size of the footer
538-
"""
539-
if not self.plot_footer_background:
540-
return
541-
542-
self.plot_footer_background.set_x(spaces.l.offset)
543-
self.plot_footer_background.set_y(spaces.b.offset)
544-
self.plot_footer_background.set_height(spaces.b.footer_height)
545-
self.plot_footer_background.set_width(spaces.plot_width)
546-
547-
def _resize_plot_footer_line(self, spaces: PlotSideSpaces):
548-
"""
549-
Resize the footer line to be a border above the footer
550-
"""
551-
if not self.plot_footer_line:
552-
return
553-
554-
x1 = spaces.l.offset
555-
x2 = x1 + spaces.plot_width
556-
y1 = y2 = spaces.b.offset + spaces.b.footer_height
557-
self.plot_footer_line.set_xdata([x1, x2])
558-
self.plot_footer_line.set_ydata([y1, y2])
559-
560550

561551
def _text_is_visible(text: Text) -> bool:
562552
"""

plotnine/_mpl/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from matplotlib.backend_bases import RendererBase
1818
from matplotlib.figure import Figure
1919
from matplotlib.gridspec import SubplotSpec
20+
from matplotlib.lines import Line2D
21+
from matplotlib.patches import Rectangle
2022
from matplotlib.text import Text
2123
from matplotlib.transforms import Transform
2224

@@ -276,6 +278,35 @@ def max_height(self, artists: Sequence[Artist]) -> float:
276278
return max(heights) if len(heights) else 0
277279

278280

281+
def resize_footer_background(
282+
background: Rectangle,
283+
x: float,
284+
y: float,
285+
height: float,
286+
width: float,
287+
):
288+
"""
289+
Resize the plot footer background to the given dimensions
290+
"""
291+
background.set_x(x)
292+
background.set_y(y)
293+
background.set_height(height)
294+
background.set_width(width)
295+
296+
297+
def resize_footer_line(
298+
line: Line2D,
299+
x: float,
300+
width: float,
301+
y: float,
302+
):
303+
"""
304+
Resize the footer line to be a horizontal border
305+
"""
306+
line.set_xdata([x, x + width])
307+
line.set_ydata([y, y])
308+
309+
279310
@dataclass
280311
class JustifyBoundaries:
281312
"""

0 commit comments

Comments
 (0)