10000 Inline iter_ticks into _update_ticks, and use that in mplot3d. · matplotlib/matplotlib@46fea7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 46fea7f

Browse files
committed
Inline iter_ticks into _update_ticks, and use that in mplot3d.
iter_ticks is only called in one place and the iterator is immediately converted into a list, so we may as well inline it and create the list immediately. Also, we can immediately assign the tick locations and labels, instead of having to carry around the ticks, the locations and the labels as a tuple. Reuse that implementation in axis3d (... which also gains support for minor ticks for free).
1 parent adda7cc commit 46fea7f

File tree

5 files changed

+58
-63
lines changed

5 files changed

+58
-63
lines changed

doc/api/axis_api.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ Ticks, tick labels and Offset text
103103
Axis.get_gridlines
104104
Axis.grid
105105

106-
Axis.iter_ticks
107106
Axis.set_tick_params
108107

109108
Axis.axis_date
@@ -365,7 +364,6 @@ YAxis
365364
YAxis.get_units
366365
YAxis.get_view_interval
367366
YAxis.grid
368-
YAxis.iter_ticks
369367
YAxis.limit_range_for_scale
370368
YAxis.pan
371369
YAxis.reset_ticks
@@ -432,7 +430,6 @@ XAxis
432430
XAxis.get_units
433431
XAxis.get_view_interval
434432
XAxis.grid
435-
XAxis.iter_ticks
436433
XAxis.limit_range_for_scale
437434
XAxis.pan
438435
XAxis.reset_ticks
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Changes to the internal tick handling API
2+
`````````````````````````````````````````
3+
4+
``Axis.iter_ticks`` (which only served as a helper to the private
5+
``Axis._update_ticks``) is deprecated.
6+
7+
The signature of the (private) ``Axis._update_ticks`` has been changed to not
8+
take the renderer as argument anymore (that argument is unused).

lib/matplotlib/axis.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ def _set_artist_props(self, a):
10161016
return
10171017
a.set_figure(self.figure)
10181018

1019+
@cbook.deprecated("3.1")
10191020
def iter_ticks(self):
10201021
"""
10211022
Yield ``(Tick, location, label)`` tuples for major and minor ticks.
@@ -1035,7 +1036,7 @@ def get_ticklabel_extents(self, renderer):
10351036
of the axes.
10361037
"""
10371038

1038-
ticks_to_draw = self._update_ticks(renderer)
1039+
ticks_to_draw = self._update_ticks()
10391040
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
10401041
renderer)
10411042

@@ -1058,20 +1059,38 @@ def get_smart_bounds(self):
10581059
"""get whether the axis has smart bounds"""
10591060
return self._smart_bounds
10601061

1061-
def _update_ticks(self, renderer):
1062+
def _update_ticks(self):
10621063
"""
1063-
Update ticks (position and labels) using the current data
1064-
interval of the axes. Returns a list of ticks that will be
1065-
drawn.
1064+
Update ticks (position and labels) using the current data interval of
1065+
the axes. Return the list of ticks that will be drawn.
10661066
"""
10671067

1068-
interval = self.get_view_interval()
1069-
tick_tups = list(self.iter_ticks()) # iter_ticks calls the locator
1070-
if self._smart_bounds and tick_tups:
1068+
major_locs = self.major.locator()
1069+
major_ticks = self.get_major_ticks(len(major_locs))
1070+
self.major.formatter.set_locs(major_locs)
1071+
major_labels = self.major.formatter.format_ticks(major_locs)
1072+
for tick, loc, label in zip(major_ticks, major_locs, major_labels):
1073+
tick.update_position(loc)
1074+
tick.set_label1(label)
1075+
tick.set_label2(label)
1076+
minor_locs = self.minor.locator()
1077+
minor_ticks = self.get_minor_ticks(len(minor_locs))
1078+
self.minor.formatter.set_locs(minor_locs)
1079+
minor_labels = self.minor.formatter.format_ticks(minor_locs)
1080+
for tick, loc, label in zip(minor_ticks, minor_locs, minor_labels):
1081+
tick.update_position(loc)
1082+
tick.set_label1(label)
1083+
tick.set_label2(label)
1084+
ticks = [*major_ticks, *minor_ticks]
1085+
1086+
view_low, view_high = self.get_view_interval()
1087+
if view_low > view_high:
1088+
view_low, view_high = view_high, view_low
1089+
1090+
if self._smart_bounds and ticks:
10711091
# handle inverted limits
1072-
view_low, view_high = sorted(interval)
10731092
data_low, data_high = sorted(self.get_data_interval())
1074-
locs = np.sort([ti[1] for ti in tick_tups])
1093+
locs = np.sort([tick.get_loc() for tick in ticks])
10751094
if data_low <= view_low:
10761095
# data extends beyond view, take view as limit
10771096
ilow = view_low
@@ -1096,33 +1115,21 @@ def _update_ticks(self, renderer):
10961115
else:
10971116
# No ticks (why not?), take last tick
10981117
ihigh = locs[-1]
1099-
tick_tups = [ti for ti in tick_tups if ilow <= ti[1] <= ihigh]
1118+
ticks = [tick for tick in ticks if ilow <= tick.get_loc() <= ihigh]
11001119

1101-
if interval[1] <= interval[0]:
1102-
interval = interval[1], interval[0]
1103-
inter = self.get_transform().transform(interval)
1120+
interval_t = self.get_transform().transform([view_low, view_high])
11041121

11051122
ticks_to_draw = []
1106-
for tick, loc, label 10000 in tick_tups:
1107-
# draw each tick if it is in interval. Note the transform
1108-
# to pixel space to take care of log transforms etc.
1109-
# interval_contains has a floating point tolerance.
1110-
if tick is None:
1111-
continue
1112-
# NB: always update labels and position to avoid issues like #9397
1113-
tick.update_position(loc)
1114-
tick.set_label1(label)
1115-
tick.set_label2(label)
1123+
for tick in ticks:
11161124
try:
1117-
loct = self.get_transform().transform(loc)
1125+
loc_t = self.get_transform().transform(tick.get_loc())
11181126
except AssertionError:
11191127
# transforms.transform doesn't allow masked values but
11201128
# some scales might make them, so we need this try/except.
1121-
loct = None
1122-
continue
1123-
if not mtransforms._interval_contains_close(inter, loct):
1124-
continue
1125-
ticks_to_draw.append(tick)
1129+
pass
1130+
else:
1131+
if mtransforms._interval_contains_close(interval_t, loc_t):
1132+
ticks_to_draw.append(tick)
11261133

11271134
return ticks_to_draw
11281135

@@ -1141,7 +1148,7 @@ def get_tightbbox(self, renderer):
11411148
if not self.get_visible():
11421149
return
11431150

1144-
ticks_to_draw = self._update_ticks(renderer)
1151+
ticks_to_draw = self._update_ticks()
11451152

11461153
self._update_label_position(renderer)
11471154

@@ -1182,7 +1189,7 @@ def draw(self, renderer, *args, **kwargs):
11821189
return
11831190
renderer.open_group(__name__)
11841191

1185-
ticks_to_draw = self._update_ticks(renderer)
1192+
ticks_to_draw = self._update_ticks()
11861193
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
11871194
renderer)
11881195

@@ -1948,7 +1955,7 @@ def _get_tick_boxes_siblings(self, renderer):
19481955
grp = self.figure._align_xlabel_grp
19491956
# if we want to align labels from other axes:
19501957
for nn, axx in enumerate(grp.get_siblings(self.axes)):
1951-
ticks_to_draw = axx.xaxis._update_ticks(renderer)
1958+
ticks_to_draw = axx.xaxis._update_ticks()
19521959
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
19531960
bboxes.extend(tlb)
19541961
bboxes2.extend(tlb2)
@@ -2262,7 +2269,7 @@ def _get_tick_boxes_siblings(self, renderer):
22622269
grp = self.figure._align_ylabel_grp
22632270
# if we want to align labels from other axes:
22642271
for axx in grp.get_siblings(self.axes):
2265-
ticks_to_draw = axx.yaxis._update_ticks(renderer)
2272+
ticks_to_draw = axx.yaxis._update_ticks()
22662273
tlb, tlb2 = axx.yaxis._get_tick_bboxes(ticks_to_draw, renderer)
22672274
bboxes.extend(tlb)
22682275
bboxes2.extend(tlb2)

lib/matplotlib/tests/test_ticker.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,15 @@ def test_offset_value(self, left, right, offset):
346346
UserWarning)
347347
ax.set_xlim(left, right)
348348
assert len(w) == (1 if left == right else 0)
349-
# Update ticks.
350-
next(ax.get_xaxis().iter_ticks())
349+
ax.get_xaxis()._update_ticks()
351350
assert formatter.offset == offset
352351

353352
with warnings.catch_warnings(record=True) as w:
354353
warnings.filterwarnings('always', 'Attempting to set identical',
355354
UserWarning)
356355
ax.set_xlim(right, left)
357356
assert len(w) == (1 if left == right else 0)
358-
# Update ticks.
359-
next(ax.get_xaxis().iter_ticks())
357+
ax.get_xaxis()._update_ticks()
360358
assert formatter.offset == offset
361359

362360
@pytest.mark.parametrize('use_offset', use_offset_data)

lib/mpl_toolkits/mplot3d/axis3d.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,11 @@ def draw(self, renderer):
223223
self.label._transform = self.axes.transData
224224
renderer.open_group('axis3d')
225225

226-
# code from XAxis
227-
majorTicks = self.get_major_ticks()
228-
majorLocs = self.major.locator()
226+
ticks = self._update_ticks()
229227

230228
info = self._axinfo
231229
index = info['i']
232230

233-
# filter locations here so that no extra grid lines are drawn
234-
locmin, locmax = self.get_view_interval()
235-
if locmin > locmax:
236-
locmin, locmax = locmax, locmin
237-
238-
# Rudimentary clipping
239-
majorLocs = [loc for loc in majorLocs if locmin <= loc <= locmax]
240-
majorLabels = self.major.formatter.format_ticks(majorLocs)
241-
242231
mins, maxs, centers, deltas, tc, highs = self._get_coord_info(renderer)
243232

244233
# Determine grid lines
@@ -259,9 +248,9 @@ def draw(self, renderer):
259248

260249
# Grid points where the planes meet
261250
xyz0 = []
262-
for val in majorLocs:
251+
for tick in ticks:
263252
coord = minmax.copy()
264-
coord[index] = val
253+
coord[index] = tick.get_loc()
265254
xyz0.append(coord)
266255

267256
# Draw labels
@@ -373,14 +362,14 @@ def draw(self, renderer):
373362
xyz1 = copy.deepcopy(xyz0)
374363
newindex = (index + 1) % 3
375364
newval = get_flip_min_max(xyz1[0], newindex, mins, maxs)
376-
for i in range(len(majorLocs)):
365+
for i in range(len(ticks)):
377366
xyz1[i][newindex] = newval
378367

379368
# Grid points at end of the other plane
380369
xyz2 = copy.deepcopy(xyz0)
381370
newindex = (index + 2) % 3
382371
newval = get_flip_min_max(xyz2[0], newindex, mins, maxs)
383-
for i in range(len(majorLocs)):
372+
for i in range(len(ticks)):
384373
xyz2[i][newindex] = newval
385374

386375
lines = list(zip(xyz1, xyz0, xyz2))
@@ -401,13 +390,11 @@ def draw(self, renderer):
401390
else:
402391
ticksign = -1
403392

404-
for tick, loc, label in zip(majorTicks, majorLocs, majorLabels):
405-
if tick is None:
406-
continue
393+
for tick in ticks:
407394

408395
# Get tick line positions
409396
pos = copy.copy(edgep1)
410-
pos[index] = loc
397+
pos[index] = tick.get_loc()
411398
pos[tickdir] = (
412399
edgep1[tickdir]
413400
+ info['tick']['outward_factor'] * ticksign * tickdelta)
@@ -434,8 +421,6 @@ def draw(self, renderer):
434421
tick_update_position(tick, (x1, x2), (y1, y2), (lx, ly))
435422
tick.tick1line.set_linewidth(info['tick']['linewidth'])
436423
tick.tick1line.set_color(info['tick']['color'])
437-
tick.set_label1(label)
438-
tick.set_label2(label)
439424
tick.draw(renderer)
440425

441426
renderer.close_group('axis3d')

0 commit comments

Comments
 (0)
0