189
189
'SymmetricalLogLocator' , 'LogitLocator' )
190
190
191
191
192
+ def _keep_in_vlim (locs , vmin , vmax , rtol = 1e-10 ):
193
+ """
194
+ trim array locs to be between vmin and vmax within
195
+ tolerance.
196
+ """
197
+ if vmin > vmax :
198
+ vmax , vmin = vmin , vmax
199
+
200
+ rtol = (vmax - vmin ) * rtol
201
+ locs = locs [locs >= vmin - rtol ]
202
+ locs = locs [locs <= vmax + rtol ]
203
+ return locs
204
+
192
205
# Work around numpy/numpy#6127.
193
206
def _divmod (x , y ):
194
207
if isinstance (x , np .generic ):
@@ -1823,37 +1836,45 @@ class MaxNLocator(Locator):
1823
1836
steps = None ,
1824
1837
integer = False ,
1825
1838
symmetric = False ,
1839
+ trim_outside = True ,
1826
1840
prune = None ,
1827
1841
min_n_ticks = 2 )
1828
1842
1829
1843
def __init__ (self , * args , ** kwargs ):
1830
1844
"""
1831
- Keyword args:
1845
+ Parameters
1846
+ ----------
1832
1847
1833
- * nbins*
1848
+ nbins : integer
1834
1849
Maximum number of intervals; one less than max number of
1835
1850
ticks. If the string `'auto'`, the number of bins will be
1836
1851
automatically determined based on the length of the axis.
1837
1852
1838
- * steps*
1853
+ steps : integer
1839
1854
Sequence of nice numbers starting with 1 and ending with 10;
1840
1855
e.g., [1, 2, 4, 5, 10], where the values are acceptable
1841
1856
tick multiples. i.e. for the example, 20, 40, 60 would be
1842
1857
an acceptable set of ticks, as would 0.4, 0.6, 0.8, because
1843
1858
they are multiples of 2. However, 30, 60, 90 would not
1844
1859
be allowed because 3 does not appear in the list of steps.
1845
1860
1846
- * integer*
1861
+ integer : bool
1847
1862
If True, ticks will take only integer values, provided
1848
1863
at least `min_n_ticks` integers are found within the
1849
1864
view limits.
1850
1865
1851
- * symmetric*
1866
+ symmetric : bool
1852
1867
If True, autoscaling will result in a range symmetric
1853
1868
about zero.
1854
1869
1855
- *prune*
1856
- ['lower' | 'upper' | 'both' | None]
1870
+ trim_outside: bool
1871
+ By default (``False``) calling ``MaxNLocator`` will return one
1872
+ tick thats less than vmin, and one tick thats greater than vmax.
1873
+ This flag suppresses that behaviour. Note its different than
1874
+ ``prune`` (below), which prunes the lower or upper tick, regardless
1875
+ of whether it is in the view limits.
1876
+
1877
+ prune : ['lower' | 'upper' | 'both' | None]
1857
1878
Remove edge ticks -- useful for stacked or ganged plots where
1858
1879
the upper tick of one axes overlaps with the lower tick of the
1859
1880
axes above it, primarily when :rc:`axes.autolimit_mode` is
@@ -1862,7 +1883,7 @@ def __init__(self, *args, **kwargs):
1862
1883
removed. If ``prune == 'both'``, the largest and smallest ticks
1863
1884
will be removed. If ``prune == None``, no ticks will be removed.
1864
1885
1865
- * min_n_ticks*
1886
+ min_n_ticks : integer
1866
1887
Relax `nbins` and `integer` constraints if necessary to
1867
1888
obtain this minimum number of ticks.
1868
1889
@@ -1910,6 +1931,9 @@ def set_params(self, **kwargs):
1910
1931
self ._nbins = int (self ._nbins )
1911
1932
if 'symmetric' in kwargs :
1912
1933
self ._symmetric = kwargs ['symmetric' ]
1934
+
1935
+ self ._trim_outside = kwargs .pop ('trim_outside' , True )
1936
+
1913
1937
if 'prune' in kwargs :
1914
1938
prune = kwargs ['prune' ]
1915
1939
if prune is not None and prune not in ['upper' , 'lower' , 'both' ]:
@@ -1993,13 +2017,17 @@ def __call__(self):
1993
2017
return self .tick_values (vmin , vmax )
1994
2018
1995
2019
def tick_values (self , vmin , vmax ):
2020
+
1996
2021
if self ._symmetric :
1997
2022
vmax = max (abs (vmin ), abs (vmax ))
1998
2023
vmin = - vmax
1999
2024
vmin , vmax = mtransforms .nonsingular (
2000
2025
vmin , vmax , expander = 1e-13 , tiny = 1e-14 )
2001
2026
locs = self ._raw_ticks (vmin , vmax )
2002
2027
2028
+ if self ._trim_outside :
2029
+ locs = _keep_in_vlim (locs , vmin , vmax )
2030
+
2003
2031
prune = self ._prune
2004
2032
if prune == 'lower' :
2005
2033
locs = locs [1 :]
@@ -2543,7 +2571,7 @@ def __init__(self):
2543
2571
else :
2544
2572
nbins = 'auto'
2545
2573
steps = [1 , 2 , 2.5 , 5 , 10 ]
2546
- MaxNLocator .__init__ (self , nbins = nbins , steps = steps )
2574
+ MaxNLocator .__init__ (self , nbins = nbins , steps = steps , trim_outside = True )
2547
2575
2548
2576
2549
2577
class AutoMinorLocator (Locator ):
0 commit comments