Skip to content

Commit fbf413c

Browse files
authored
Fix tick visibility leaking from styles in alternative axes (#721)
When applying custom styles like dark_background, matplotlib's styling dictionaries often include explicit boolean flags for tick visibility on all sides (e.g. left, right, top, bottom). Previously, UltraPlot queried these style settings using _get_tickline_props and inadvertently reapplied them to the axes, completely overriding UltraPlot's internal locators. This caused bugs such as the left axis displaying right ticks and the right axis displaying left ticks when a style was active. By aggressively popping visibility keys out of the retrieved styling dictionary, we ensure that themes strictly govern visual appearances (color, width, padding) without hijacking the structural tick visibility correctly managed by the format methods.
1 parent 8e1228c commit fbf413c

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

ultraplot/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,13 @@ def _get_tickline_props(self, axis=None, which="major", native=True, rebuild=Fal
12961296
context = not rebuild and (native or self._context_mode == 2)
12971297
kwticks = self.category(f"{axis}tick.{which}", context=context)
12981298
kwticks.pop("visible", None)
1299+
1300+
# NOTE: We pop visibility properties from the styling dictionary so that
1301+
# stylistic updates (like applying a dark_background theme) do not override
1302+
# the tick visibility logic strictly managed by ax._update_locs() and alternate axes.
1303+
for key in ("bottom", "top", "left", "right"):
1304+
kwticks.pop(key, None)
1305+
12991306
for key in ("color", "direction"):
13001307
value = self.find(f"{axis}tick.{key}", context=context)
13011308
if value is not None:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
import ultraplot as uplt
3+
4+
5+
def test_alt_axes_styling_dark_background():
6+
"""
7+
Test that applying dark_background style does not leak tick visibility
8+
settings and correctly preserves alternative axes tick locations.
9+
"""
10+
with uplt.rc.context(style="dark_background"):
11+
fig, ax = uplt.subplots()
12+
ax.format(ycolor="C0", ylabel="Left Axis")
13+
14+
ax2 = ax.alty(color="C1")
15+
ax2.format(ycolor="C1", ylabel="Right Axis", ylim=(0, 1))
16+
17+
# The left axis should ONLY have visible ticks on the left
18+
left_ax_left_ticks = sum(
19+
1
20+
for t in ax.yaxis.get_ticklines()
21+
if t.get_visible() and t.get_xdata()[0] == 0
22+
)
23+
left_ax_right_ticks = sum(
24+
1
25+
for t in ax.yaxis.get_ticklines()
26+
if t.get_visible() and t.get_xdata()[0] == 1
27+
)
28+
29+
# The right axis (ax2) should ONLY have visible ticks on the right
30+
right_ax_left_ticks = sum(
31+
1
32+
for t in ax2.yaxis.get_ticklines()
33+
if t.get_visible() and t.get_xdata()[0] == 0
34+
)
35+
right_ax_right_ticks = sum(
36+
1
37+
for t in ax2.yaxis.get_ticklines()
38+
if t.get_visible() and t.get_xdata()[0] == 1
39+
)
40+
41+
assert left_ax_left_ticks > 0, "Left axis should have left ticks"
42+
assert left_ax_right_ticks == 0, "Left axis should NOT have right ticks"
43+
44+
assert right_ax_left_ticks == 0, "Right axis should NOT have left ticks"
45+
assert right_ax_right_ticks > 0, "Right axis should have right ticks"

0 commit comments

Comments
 (0)