Added option for an offset for MultipleLocator · matplotlib/matplotlib@f386f33 · GitHub
Skip to content

Commit f386f33

Browse files
jondoesntgitrcomer
authored andcommitted
Added option for an offset for MultipleLocator
Co-authored-by: Ruth Comer 10599679+rcomer@users.noreply.github.com
1 parent a844eca commit f386f33

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

lib/matplotlib/tests/test_ticker.py

Lines changed: 17 additions & 0 deletions

lib/matplotlib/ticker.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,17 +1831,41 @@ def view_limits(self, vmin, vmax):
18311831

18321832
class MultipleLocator(Locator):
18331833
"""
1834-
Set a tick on each integer multiple of the *base* within the view
1835-
interval.
1834+
Set a tick on each integer multiple of the *base* plus an *offset* within
1835+
the view interval.
18361836
"""
18371837

1838-
def __init__(self, base=1.0):
1838+
def __init__(self, base=1.0, offset=0.0):
1839+
"""
1840+
Parameters
1841+
----------
1842+
base : float > 0
1843+
Interval between ticks.
1844+
offset : float
1845+
Value added to each multiple of *base*.
1846+
1847+
.. versionadded:: 3.8
1848+
"""
18391849
self._edge = _Edge_integer(base, 0)
1850+
self._offset = offset
18401851

1841-
def set_params(self, base):
1842-
"""Set parameters within this locator."""
1852+
def set_params(self, base=None, offset=None):
1853+
"""
1854+
Set parameters within this locator.
1855+
1856+
Parameters
1857+
----------
1858+
base : float > 0
1859+
Interval between ticks.
1860+
offset : float
1861+
Value added to each multiple of *base*.
1862+
1863+
.. versionadded:: 3.8
1864+
"""
18431865
if base is not None:
18441866
self._edge = _Edge_integer(base, 0)
1867+
if offset is not None:
1868+
self._offset = offset
18451869

18461870
def __call__(self):
18471871
"""Return the locations of the ticks."""
@@ -1852,19 +1876,20 @@ def tick_values(self, vmin, vmax):
18521876
if vmax < vmin:
18531877
vmin, vmax = vmax, vmin
18541878
step = self._edge.step
1879+
vmin -= self._offset
1880+
vmax -= self._offset
18551881
vmin = self._edge.ge(vmin) * step
18561882
n = (vmax - vmin + 0.001 * step) // step
1857-
locs = vmin - step + np.arange(n + 3) * step
1883+
locs = vmin - step + np.arange(n + 3) * step + self._offset
18581884
return self.raise_if_exceeds(locs)
18591885

18601886
def view_limits(self, dmin, dmax):
18611887
"""
1862-
Set the view limits to the nearest multiples of *base* that
1863-
contain the data.
1888+
Set the view limits to the nearest tick values that contain the data.
18641889
"""
18651890
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
1866-
vmin = self._edge.le(dmin) * self._edge.step
1867-
vmax = self._edge.ge(dmax) * self._edge.step
1891+
vmin = self._edge.le(dmin - self._offset) * self._edge.step + self._offset
1892+
vmax = self._edge.ge(dmax - self._offset) * self._edge.step + self._offset
18681893
if vmin == vmax:
18691894
vmin -= 1
18701895
vmax += 1

lib/matplotlib/ticker.pyi

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)