Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8571230

Browse filesBrowse files
committed
Exposed the Locator API for ease of use without an Axis.
1 parent 5154655 commit 8571230
Copy full SHA for 8571230

File tree

Expand file treeCollapse file tree

1 file changed

+80
-21
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+80
-21
lines changed

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+80-21Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,31 @@ class Locator(TickHelper):
819819
# This parameter is set to cause locators to raise an error if too
820820
# many ticks are generated
821821
MAXTICKS = 1000
822-
def __call__(self):
823-
'Return the locations of the ticks'
822+
823+
def tick_values(self, vmin, vmax):
824+
"""
825+
Return the values of the located ticks given **vmin** and **vmax**.
826+
827+
.. note::
828+
To get tick locations with the vmin and vmax values defined
829+
automatically for the associated :attr:`axis` simply call
830+
the Locator instance::
831+
832+
>>> print(type(loc))
833+
<type 'Locator'>
834+
>>> print(loc())
835+
[1, 2, 3, 4]
836+
837+
"""
824838
raise NotImplementedError('Derived must override')
825839

840+
def __call__(self):
841+
"""Return the locations of the ticks"""
842+
vmin, vmax = self.axis.get_view_interval()
843+
self.tick_values(vmin, vmax)
844+
826845
def raise_if_exceeds(self, locs):
827-
'raise a RuntimeError if Locator attempts to create more than MAXTICKS locs'
846+
"""raise a RuntimeError if Locator attempts to create more than MAXTICKS locs"""
828847
if len(locs)>=self.MAXTICKS:
829848
msg = ('Locator attempting to generate %d ticks from %s to %s: ' +
830849
'exceeds Locator.MAXTICKS') % (len(locs), locs[0], locs[-1])
@@ -841,11 +860,11 @@ def view_limits(self, vmin, vmax):
841860
return mtransforms.nonsingular(vmin, vmax)
842861

843862
def autoscale(self):
844-
'autoscale the view limits'
863+
"""autoscale the view limits"""
845864
return self.view_limits(*self.axis.get_view_interval())
846865

847866
def pan(self, numsteps):
848-
'Pan numticks (can be positive or negative)'
867+
"""Pan numticks (can be positive or negative)"""
849868
ticks = self()
850869
numticks = len(ticks)
851870

@@ -872,7 +891,7 @@ def zoom(self, direction):
872891
self.axis.set_view_interval(vmin + step, vmax - step, ignore=True)
873892

874893
def refresh(self):
875-
'refresh internal information based on current lim'
894+
"""refresh internal information based on current lim"""
876895
pass
877896

878897

@@ -889,8 +908,11 @@ def __init__(self, base, offset):
889908
self.offset = offset
890909

891910
def __call__(self):
892-
'Return the locations of the ticks'
911+
"""Return the locations of the ticks"""
893912
dmin, dmax = self.axis.get_data_interval()
913+
return self.tick_values(dmin, dmax)
914+
915+
def tick_values(self, dmin, dmax):
894916
return self.raise_if_exceeds(
895917
np.arange(dmin + self.offset, dmax+1, self._base))
896918

@@ -913,7 +935,17 @@ def __init__(self, locs, nbins=None):
913935
self.nbins = max(self.nbins, 2)
914936

915937
def __call__(self):
916-
'Return the locations of the ticks'
938+
return self.tick_values(None, None)
939+
940+
def tick_values(self, dmin, dmax):
941+
""""
942+
Return the locations of the ticks.
943+
944+
.. note::
945+
946+
Because the values are fixed, dmin and dmax are not used in this method.
947+
948+
"""
917949
if self.nbins is None:
918950
return self.locs
919951
step = max(int(0.99 + len(self.locs) / float(self.nbins)), 1)
@@ -925,17 +957,26 @@ def __call__(self):
925957
return self.raise_if_exceeds(ticks)
926958

927959

928-
929-
930960
class NullLocator(Locator):
931961
"""
932962
No ticks
933963
"""
934964

935965
def __call__(self):
936-
'Return the locations of the ticks'
966+
return self.tick_values(None, None)
967+
968+
def tick_values(self, dmin, dmax):
969+
""""
970+
Return the locations of the ticks.
971+
972+
.. note::
973+
974+
Because the values are Null, dmin and dmax are not used in this method.
975+
976+
"""
937977
return []
938978

979+
939980
class LinearLocator(Locator):
940981
"""
941982
Determine the tick locations
@@ -944,9 +985,8 @@ class LinearLocator(Locator):
944985
number of ticks to make a nice tick partitioning. Thereafter the
945986
number of ticks will be fixed so that interactive navigation will
946987
be nice
947-
"""
948-
949988
989+
"""
950990
def __init__(self, numticks = None, presets=None):
951991
"""
952992
Use presets to set locs based on lom. A dict mapping vmin, vmax->locs
@@ -959,8 +999,10 @@ def __init__(self, numticks = None, presets=None):
959999

9601000
def __call__(self):
9611001
'Return the locations of the ticks'
962-
9631002
vmin, vmax = self.axis.get_view_interval()
1003+
return self.tick_values(vmin, vmax)
1004+
1005+
def tick_values(self, vmin, vmax):
9641006
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander = 0.05)
9651007
if vmax<vmin:
9661008
vmin, vmax = vmax, vmin
@@ -971,14 +1013,11 @@ def __call__(self):
9711013
if self.numticks is None:
9721014
self._set_numticks()
9731015

974-
975-
9761016
if self.numticks==0: return []
9771017
ticklocs = np.linspace(vmin, vmax, self.numticks)
9781018

9791019
return self.raise_if_exceeds(ticklocs)
9801020

981-
9821021
def _set_numticks(self):
9831022
self.numticks = 11 # todo; be smart here; this is just for dev
9841023

@@ -1007,6 +1046,7 @@ def closeto(x,y):
10071046
if abs(x-y)<1e-10: return True
10081047
else: return False
10091048

1049+
10101050
class Base:
10111051
'this solution has some hacks to deal with floating point inaccuracies'
10121052
def __init__(self, base):
@@ -1046,6 +1086,7 @@ def ge(self, x):
10461086
def get_base(self):
10471087
return self._base
10481088

1089+
10491090
class MultipleLocator(Locator):
10501091
"""
10511092
Set a tick on every integer that is multiple of base in the
@@ -1058,6 +1099,9 @@ def __init__(self, base=1.0):
10581099
def __call__(self):
10591100
'Return the locations of the ticks'
10601101
vmin, vmax = self.axis.get_view_interval()
1102+
return self.tick_values(vmin, vmax)
1103+
1104+
def tick_values(self, vmin, vmax):
10611105
if vmax<vmin:
10621106
vmin, vmax = vmax, vmin
10631107
vmin = self._base.ge(vmin)
@@ -1209,6 +1253,9 @@ def bin_boundaries(self, vmin, vmax):
12091253

12101254
def __call__(self):
12111255
vmin, vmax = self.axis.get_view_interval()
1256+
return self.tick_values(vmin, vmax)
1257+
1258+
def tick_values(self, vmin, vmax):
12121259
vmin, vmax = mtransforms.nonsingular(vmin, vmax, expander = 1e-13,
12131260
tiny=1e-14)
12141261
locs = self.bin_boundaries(vmin, vmax)
@@ -1294,10 +1341,11 @@ def subs(self,subs):
12941341

12951342
def __call__(self):
12961343
'Return the locations of the ticks'
1297-
b=self._base
1298-
12991344
vmin, vmax = self.axis.get_view_interval()
1345+
return self.tick_values(vmin, vmax)
13001346

1347+
def tick_values(self, vmin, vmax):
1348+
b=self._base
13011349
# dummy axis has no axes attribute
13021350
if hasattr(self.axis, 'axes') and self.axis.axes.name == 'polar':
13031351
vmax = math.ceil(math.log(vmax) / math.log(b))
@@ -1395,11 +1443,14 @@ def __init__(self, transform, subs=None):
13951443

13961444
def __call__(self):
13971445
'Return the locations of the ticks'
1446+
# Note, these are untransformed coordinates
1447+
vmin, vmax = self.axis.get_view_interval()
1448+
return self.tick_values(vmin, vmax)
1449+
1450+
def tick_values(self, vmin, vmax):
13981451
b = self._transform.base
13991452
t = self._transform.linthresh
14001453

1401-
# Note, these are untransformed coordinates
1402-
vmin, vmax = self.axis.get_view_interval()
14031454
if vmax < vmin:
14041455
vmin, vmax = vmax, vmin
14051456

@@ -1588,6 +1639,10 @@ def __call__(self):
15881639

15891640
return self.raise_if_exceeds(np.array(locs))
15901641

1642+
def tick_values(self, vmin, vmax):
1643+
raise NotImplementedError('Cannot get tick locations for a '
1644+
'%s type.' % type(self))
1645+
15911646

15921647
class OldAutoLocator(Locator):
15931648
"""
@@ -1603,6 +1658,10 @@ def __call__(self):
16031658
self.refresh()
16041659
return self.raise_if_exceeds(self._locator())
16051660

1661+
def tick_values(self, vmin, vmax):
1662+
raise NotImplementedError('Cannot get tick locations for a '
1663+
'%s type.' % type(self))
1664+
16061665
def refresh(self):
16071666
'refresh internal information based on current lim'
16081667
vmin, vmax = self.axis.get_view_interval()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.