Skip to content

Commit 17eeeb6

Browse files
committed
fix(facet): skip tick-label padding when axis text is blank
theme(text=element_blank()) removes the specific axis-text themeables, so theme.get_margin('axis_text_x') resolves to None and set_limits_breaks_and_labels crashed on None.pt. Guard each axis with theme.T.is_blank(...) and skip tick_params when the axis text is blank (it is not drawn, so the pad is irrelevant).
1 parent 0795dd7 commit 17eeeb6

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

plotnine/facets/facet.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,14 @@ def _inf_to_none(
350350
ax.xaxis.set_major_formatter(MyFixedFormatter(panel_params.x.labels))
351351
ax.yaxis.set_major_formatter(MyFixedFormatter(panel_params.y.labels))
352352

353-
pad_x = theme.get_margin("axis_text_x").pt.t
354-
pad_y = theme.get_margin("axis_text_y").pt.r
355-
356-
ax.tick_params(axis="x", which="major", pad=pad_x)
357-
ax.tick_params(axis="y", which="major", pad=pad_y)
353+
# Blank axis text is not drawn, so its margin may be absent
354+
# (resolves to None). Skip the tick-label padding in that case.
355+
if not theme.T.is_blank("axis_text_x"):
356+
pad_x = theme.get_margin("axis_text_x").pt.t
357+
ax.tick_params(axis="x", which="major", pad=pad_x)
358+
if not theme.T.is_blank("axis_text_y"):
359+
pad_y = theme.get_margin("axis_text_y").pt.r
360+
ax.tick_params(axis="y", which="major", pad=pad_y)
358361

359362
def __deepcopy__(self, memo: dict[Any, Any]) -> facet:
360363
"""

tests/test_theme.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,11 @@ def test_override_axis_text():
337337
)
338338

339339
assert p == "override_axis_text"
340+
341+
342+
def test_blank_all_text_draws():
343+
# Blanking the base `text` element removes every specific text
344+
# themeable, so axis_text_x/_y margins resolve to None. Drawing must
345+
# not crash when computing the tick-label padding. (Regression)
346+
p = ggplot() + lims(x=(0, 100), y=(0, 100)) + theme(text=element_blank())
347+
p.draw_test() # pyright: ignore # must not raise

0 commit comments

Comments
 (0)