Skip to content

Commit e4ada78

Browse files
authored
explicitly override minor locator if given (#96)
* explicitly override minor locator if given * removed print statement * added comment * black formatting * added comments to unittest * add return
1 parent eea3824 commit e4ada78

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ultraplot/axes/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ def _add_colorbar(
11591159
locator = mticker.FixedLocator(ticks)
11601160
else:
11611161
locator = pticker.DiscreteLocator(ticks)
1162+
11621163
if tickminor and minorlocator is None:
11631164
minorlocator = pticker.DiscreteLocator(ticks, minor=True)
11641165

@@ -1213,6 +1214,10 @@ def _add_colorbar(
12131214
# obj.minorlocator = minorlocator # backwards compatibility
12141215
obj.update_ticks = guides._update_ticks.__get__(obj) # backwards compatible
12151216
if minorlocator is not None:
1217+
# Note we make use of mpl's setters and getters
1218+
current = obj.minorlocator
1219+
if current != minorlocator:
1220+
obj.minorlocator = minorlocator
12161221
obj.update_ticks()
12171222
elif tickminor:
12181223
obj.minorticks_on()

ultraplot/tests/test_colorbar.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,20 @@ def test_reversed_levels():
251251
ax.pcolormesh(data, colorbar="b", **kw)
252252
i += 1
253253
return fig
254+
255+
256+
@pytest.mark.mpl_image_compare
257+
def test_minor_override():
258+
"""Test minor ticks override."""
259+
# Setting a custom minor tick should override the settings. Here we set the ticks to 1 and the minorticks to half that. We then check that the minor ticks are set correctly
260+
data = state.rand(10, 10)
261+
left, right, minor, n = 0, 1, 0.05, 11
262+
levels = np.linspace(left, right, n)
263+
fig, ax = uplt.subplots()
264+
m = ax.pcolormesh(data, colorbar="b", levels=levels)
265+
cax = ax.colorbar(m, minorticks=minor)
266+
assert np.allclose(
267+
cax.minorlocator.tick_values(left, right),
268+
np.linspace(left - minor, right + minor, n * 2 + 1),
269+
)
270+
return fig

0 commit comments

Comments
 (0)