|
1 | 1 | import numpy as np |
| 2 | +import pandas as pd |
2 | 3 | import pytest |
3 | 4 | from PIL import Image, ImageDraw |
4 | 5 |
|
5 | 6 | from plotnine import ( |
| 7 | + aes, |
| 8 | + coord_cartesian, |
6 | 9 | element_line, |
7 | 10 | element_rect, |
8 | 11 | element_text, |
9 | 12 | facet_wrap, |
| 13 | + geom_col, |
| 14 | + ggplot, |
10 | 15 | labs, |
11 | 16 | theme, |
| 17 | + theme_void, |
12 | 18 | ) |
13 | 19 | from plotnine._utils.yippie import geom as g |
14 | 20 | from plotnine._utils.yippie import plot |
15 | 21 | from plotnine.composition import inset_element, plot_annotation |
16 | 22 | from plotnine.exceptions import PlotnineWarning |
| 23 | +from plotnine.themes.elements import margin |
17 | 24 |
|
18 | 25 |
|
19 | 26 | def _smiley() -> np.ndarray: |
@@ -382,32 +389,58 @@ class TestFooterInset: |
382 | 389 | align_to="footer" maps the inset bbox onto the footer band |
383 | 390 | """ |
384 | 391 |
|
| 392 | + p = ( |
| 393 | + plot.white |
| 394 | + + labs(footer="Source: Example Corp") |
| 395 | + + theme( |
| 396 | + plot_margin=0.01, |
| 397 | + plot_footer=element_text( |
| 398 | + margin=margin(t=0.01, b=0.01, unit="fig") |
| 399 | + ), |
| 400 | + plot_footer_background=element_rect(fill="#E9E9E9"), |
| 401 | + ) |
| 402 | + ) |
| 403 | + |
385 | 404 | def test_footer_logo_right_aligned(self): |
386 | 405 | # A logo placed in the right portion of the footer band, inline |
387 | 406 | # with left-justified footer text. |
388 | | - p = ( |
389 | | - plot.white |
390 | | - + g.points |
391 | | - + labs(footer="Source: Example Corp") |
392 | | - + inset_element( |
393 | | - SMILEY_FACE, |
394 | | - left=0.9, |
395 | | - bottom=0.0, |
396 | | - right=1.0, |
397 | | - top=1.0, |
398 | | - align_to="footer", |
399 | | - ) |
| 407 | + p = self.p + inset_element( |
| 408 | + SMILEY_FACE, |
| 409 | + left=0.85, |
| 410 | + bottom=0.15, |
| 411 | + right=1 - 0.01, |
| 412 | + top=0.85, |
| 413 | + align_to="footer", |
| 414 | + anchor="right", |
400 | 415 | ) |
401 | 416 | assert p == "footer_logo_right_aligned" |
402 | 417 |
|
| 418 | + def test_footer_plot_right_aligned(self): |
| 419 | + # A plot placed in the right portion of the footer band, inline |
| 420 | + # with left-justified footer text. |
| 421 | + data = pd.DataFrame({"x": ["a", "b", "c"], "y": [1, 2, 3]}) |
| 422 | + p_inset = ( |
| 423 | + ggplot(data, aes("x", "y", fill="x")) |
| 424 | + + geom_col(show_legend=False) |
| 425 | + + coord_cartesian(expand=False) |
| 426 | + + theme_void() |
| 427 | + ) |
| 428 | + p = self.p + inset_element( |
| 429 | + p_inset, |
| 430 | + left=0.85, |
| 431 | + bottom=0.15, |
| 432 | + right=1 - 0.01, |
| 433 | + top=0.85, |
| 434 | + align_to="footer", |
| 435 | + ) |
| 436 | + assert p == "footer_plot_right_aligned" |
| 437 | + |
403 | 438 | def test_footer_inset_without_footer_text(self): |
404 | 439 | # No footer text -> degenerate footer band. The inset is skipped |
405 | 440 | # and a warning is emitted; the rendered plot is just the host. |
406 | 441 | # The == comparison draws the plot, which triggers the warning. |
407 | | - p = ( |
408 | | - plot.white |
409 | | - + g.points |
410 | | - + inset_element(SMILEY_FACE, 0.9, 0.0, 1.0, 1.0, align_to="footer") |
| 442 | + p = plot.white + inset_element( |
| 443 | + SMILEY_FACE, 0.9, 0.0, 1.0, 1.0, align_to="footer" |
411 | 444 | ) |
412 | 445 | with pytest.warns(PlotnineWarning, match="no footer text"): |
413 | 446 | assert p == "footer_inset_no_footer_text" |
0 commit comments