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

Skip to content

Commit de02706

Browse files
committed
move label logic to independent function
1 parent 5ff769a commit de02706

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/matplotlib/axis.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,15 @@ def iter_ticks(self):
889889
"""
890890
Iterate through all of the major and minor ticks.
891891
"""
892-
majorLocs, hasLabel = self.major.locator()
892+
majorLocs = self.major.locator()
893+
hasLabel = self.major.locator.show_tick_label(majorLocs)
893894
majorTicks = self.get_major_ticks(len(majorLocs))
894895
self.major.formatter.set_locs(majorLocs)
895896
majorLabels = [self.major.formatter(val, i) if hasLabel[i] else ''
896897
for i, val in enumerate(majorLocs)]
897898

898-
minorLocs, hasLabel = self.minor.locator()
899+
minorLocs = self.minor.locator()
900+
hasLabel = self.major.locator.show_tick_label(minorLocs)
899901
minorTicks = self.get_minor_ticks(len(minorLocs))
900902
self.minor.formatter.set_locs(minorLocs)
901903
minorLabels = [self.minor.formatter(val, i) if hasLabel[i] else ''

lib/matplotlib/ticker.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,11 @@ def __call__(self):
10521052
# hence there is no *one* interface to call self.tick_values.
10531053
raise NotImplementedError('Derived must override')
10541054

1055+
def show_tick_label(self, locs):
1056+
"""Return boolean array on whether to show a label for the given
1057+
locations"""
1058+
return np.ones(loc.size, dtype=np.bool)
1059+
10551060
def raise_if_exceeds(self, locs):
10561061
"""raise a RuntimeError if Locator attempts to create more than
10571062
MAXTICKS locs"""
@@ -1658,6 +1663,17 @@ def tick_values(self, vmin, vmax):
16581663
else:
16591664
ticklocs = b ** decades
16601665

1666+
return self.raise_if_exceeds(np.asarray(ticklocs))
1667+
1668+
def show_tick_label(self, ticklocs):
1669+
b = self._base
1670+
1671+
vmin, vmax = self.axis.get_view_interval()
1672+
vmin = math.log(vmin) / math.log(b)
1673+
vmax = math.log(vmax) / math.log(b)
1674+
1675+
numdec = abs(vmax - vmin)
1676+
16611677
if numdec > 3:
16621678
sublabel = set((1))
16631679
elif numdec > 2:
@@ -1667,14 +1683,14 @@ def tick_values(self, vmin, vmax):
16671683
else:
16681684
sublabel = set((1, 2, 4, 7))
16691685

1670-
label = [True,] * np.asarray(ticklocs).size
1686+
label = np.ones(ticklocs.size, dtype=np.bool)
16711687
for i, loc in enumerate(ticklocs):
16721688
exponent = math.floor(math.log(abs(loc)) / math.log(b))
16731689
coeff = loc / b ** nearest_long(exponent)
16741690
if nearest_long(coeff) not in sublabel:
16751691
label[i] = False
16761692

1677-
return self.raise_if_exceeds(np.asarray(ticklocs)), label
1693+
return label
16781694

16791695
def view_limits(self, vmin, vmax):
16801696
'Try to choose the view limits intelligently'

0 commit comments

Comments
 (0)