Skip to content

Commit 1045dd1

Browse files
committed
Fix overriding blanked out axis_text
fixes #931
1 parent e4a3226 commit 1045dd1

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

doc/changelog.qmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ title: Changelog
102102

103103
- Fixed theming of minor x axis ticks.
104104

105+
- Fixed theming of `axis_text`, `axis_text_x` and `axis_text_y` so when they can be
106+
overriden if previously set to `element_blank()`.
107+
105108
## v0.14.5
106109
(2025-01-02)
107110
[![](https://zenodo.org/badge/DOI/10.5281/zenodo.14587381.svg)](https://doi.org/10.5281/zenodo.14587381)

plotnine/themes/themeable.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,28 @@ class axis_text_x(MixinSequenceOfValues):
894894

895895
def apply_ax(self, ax: Axes):
896896
super().apply_ax(ax)
897-
self.set(ax.get_xticklabels())
897+
898+
# TODO: Remove this code when the minimum matplotlib >= 3.10.0,
899+
# and use the commented one below it
900+
import matplotlib as mpl
901+
from packaging import version
902+
903+
vinstalled = version.parse(mpl.__version__)
904+
v310 = version.parse("3.10.0")
905+
name = "labelbottom" if vinstalled >= v310 else "labelleft"
906+
if not ax.xaxis.get_tick_params()[name]:
907+
return
908+
909+
# if not ax.xaxis.get_tick_params()["labelbottom"]:
910+
# return
911+
912+
labels = [t.label1 for t in ax.xaxis.get_major_ticks()]
913+
self.set(labels)
898914

899915
def blank_ax(self, ax: Axes):
900916
super().blank_ax(ax)
901-
ax.xaxis.set_tick_params(
902-
which="both", labelbottom=False, labeltop=False
903-
)
917+
for t in ax.xaxis.get_major_ticks():
918+
t.label1.set_visible(False)
904919

905920

906921
class axis_text_y(MixinSequenceOfValues):
@@ -927,13 +942,17 @@ class axis_text_y(MixinSequenceOfValues):
927942

928943
def apply_ax(self, ax: Axes):
929944
super().apply_ax(ax)
930-
self.set(ax.get_yticklabels())
945+
946+
if not ax.yaxis.get_tick_params()["labelleft"]:
947+
return
948+
949+
labels = [t.label1 for t in ax.yaxis.get_major_ticks()]
950+
self.set(labels)
931951

932952
def blank_ax(self, ax: Axes):
933953
super().blank_ax(ax)
934-
ax.yaxis.set_tick_params(
935-
which="both", labelleft=False, labelright=False
936-
)
954+
for t in ax.yaxis.get_major_ticks():
955+
t.label1.set_visible(False)
937956

938957

939958
class axis_text(axis_text_x, axis_text_y):
Loading

tests/test_theme.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
geom_point,
1515
ggplot,
1616
labs,
17+
lims,
1718
theme,
1819
theme_538,
1920
theme_bw,
@@ -269,3 +270,16 @@ def test_theme_xkcd():
269270
assert p != "theme_gray"
270271
else:
271272
assert p == "theme_xkcd"
273+
274+
275+
def test_override_axis_text():
276+
p = (
277+
ggplot()
278+
+ lims(x=(0, 100), y=(0, 100))
279+
+ theme(
280+
axis_text=element_blank(),
281+
axis_text_x=element_text(color="purple", margin={"t": 4}),
282+
)
283+
)
284+
285+
assert p == "override_axis_text"

0 commit comments

Comments
 (0)