Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97eceb1

Browse files
committed
Move major/minor tick overstrike logic to Axis.
The Axis knows what the major and the minor ticker are so it makes sense to let it handle the deduplication logic. Revert some heuristic deduplication logic from the Locators themselves (which effectively tests that the new code works).
1 parent d3e8ce3 commit 97eceb1

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Minor Locator no longer try to avoid overstriking major Locators
2+
````````````````````````````````````````````````````````````````
3+
4+
Previously, certain locator classes (`LogLocator`, `AutoMinorLocator`)
5+
contained custom logic to avoid emitting tick locations that collided with
6+
major ticks when they were used as minor locators.
7+
8+
This logic has now moved to the Axis class; thus, for example,
9+
``xaxis.minor.locator()`` now includes positions that collide with
10+
``xaxis.major.locator()``, but ``xaxis.get_minorticklocs()`` does not.

lib/matplotlib/axis.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,15 +1015,13 @@ def iter_ticks(self):
10151015
"""
10161016
Iterate through all of the major and minor ticks.
10171017
"""
1018-
major_locs = self.major.locator()
1019-
major_ticks = self.get_major_ticks(len(major_locs))
1018+
major_locs = self.get_majorticklocs()
10201019
major_labels = self.major.formatter.format_ticks(major_locs)
1021-
1022-
minor_locs = self.minor.locator()
1023-
minor_ticks = self.get_minor_ticks(len(minor_locs))
1024-
minor_labels = self.minor.formatter.format_ticks(minor_locs)
1025-
1020+
major_ticks = self.get_major_ticks(len(major_locs))
10261021
yield from zip(major_ticks, major_locs, major_labels)
1022+
minor_locs = self.get_minorticklocs()
1023+
minor_labels = self.minor.formatter.format_ticks(minor_locs)
1024+
minor_ticks = self.get_minor_ticks(len(minor_locs))
10271025
yield from zip(minor_ticks, minor_locs, minor_labels)
10281026

10291027
def get_ticklabel_extents(self, renderer):
@@ -1304,18 +1302,29 @@ def get_ticklines(self, minor=False):
13041302
return self.get_majorticklines()
13051303

13061304
def get_majorticklocs(self):
1307-
"Get the major tick locations in data coordinates as a numpy array"
1305+
"""Get the array of major tick locations in data coordinates."""
13081306
return self.major.locator()
13091307

13101308
def get_minorticklocs(self):
1311-
"Get the minor tick locations in data coordinates as a numpy array"
1312-
return self.minor.locator()
1309+
"""Get the array of minor tick locations in data coordinates."""
1310+
# Remove minor ticks duplicating major ticks.
1311+
major_locs = self.major.locator()
1312+
minor_locs = self.minor.locator()
1313+
transform = self._scale.get_transform()
1314+
tr_minor_locs = transform.transform(minor_locs)
1315+
tr_major_locs = transform.transform(major_locs)
1316+
lo, hi = sorted(transform.transform(self.get_view_interval()))
1317+
# Use the transformed view limits as scale. 1e-5 is the default rtol
1318+
# for np.isclose.
1319+
tol = (hi - lo) * 1e-5
1320+
minor_locs = [
1321+
loc for loc, tr_loc in zip(minor_locs, tr_minor_locs)
1322+
if not np.isclose(tr_loc, tr_major_locs, atol=tol, rtol=0).any()]
1323+
return minor_locs
13131324

13141325
def get_ticklocs(self, minor=False):
1315-
"Get the tick locations in data coordinates as a numpy array"
1316-
if minor:
1317-
return self.minor.locator()
1318-
return self.major.locator()
1326+
"""Get the array of tick locations in data coordinates."""
1327+
return self.get_minorticklocs() if minor else self.get_majorticklocs()
13191328

13201329
def get_ticks_direction(self, minor=False):
13211330
"""

lib/matplotlib/ticker.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,13 +2257,9 @@ def tick_values(self, vmin, vmax):
22572257
# If we're a minor locator *that expects at least two ticks per
22582258
# decade* and the major locator stride is 1 and there's no more
22592259
# than one minor tick, switch to AutoLocator.
2260-
ticklocs = AutoLocator().tick_values(vmin, vmax)
2261-
# Don't overstrike the major labels. Assumes major locs are
2262-
# at b = self._base
2263-
ticklocs = ticklocs[
2264-
~is_close_to_int(np.log(ticklocs) / np.log(b))]
2265-
return ticklocs
2266-
return self.raise_if_exceeds(ticklocs)
2260+
return AutoLocator().tick_values(vmin, vmax)
2261+
else:
2262+
return self.raise_if_exceeds(ticklocs)
22672263

22682264
def view_limits(self, vmin, vmax):
22692265
'Try to choose the view limits intelligently'
@@ -2644,10 +2640,6 @@ def __call__(self):
26442640
tmin = ((vmin - t0) // minorstep + 1) * minorstep
26452641
tmax = ((vmax - t0) // minorstep + 1) * minorstep
26462642
locs = np.arange(tmin, tmax, minorstep) + t0
2647-
mod = np.abs((locs - t0) % majorstep)
2648-
cond1 = mod > minorstep / 10.0
2649-
cond2 = ~np.isclose(mod, majorstep, atol=0)
2650-
locs = locs[cond1 & cond2]
26512643

26522644
return self.raise_if_exceeds(locs)
26532645

0 commit comments

Comments
 (0)