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

Skip to content

Commit 17509c2

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 922dea2 commit 17509c2

File tree

5 files changed

+58
-66
lines changed

5 files changed

+58
-66
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._uodate_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
@@ -1012,6 +1012,7 @@ def _set_artist_props(self, a):
10121012
return
10131013
a.set_figure(self.figure)
10141014

1015+
@cbook.deprecated("3.1")
10151016
def iter_ticks(self):
10161017
"""
10171018
Iterate through all of the 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 = sorted(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 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

@@ -1186,7 +1193,7 @@ def draw(self, renderer, *args, **kwargs):
11861193
return
11871194
renderer.open_group(__name__)
11881195

1189-
ticks_to_draw = self._update_ticks(renderer)
1196+
ticks_to_draw = self._update_ticks()
11901197
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
11911198
renderer)
11921199

@@ -1979,7 +1986,7 @@ def _get_tick_boxes_siblings(self, renderer):
19791986
grp = self.figure._align_xlabel_grp
19801987
# if we want to align labels from other axes:
19811988
for nn, axx in enumerate(grp.get_siblings(self.axes)):
1982-
ticks_to_draw = axx.xaxis._update_ticks(renderer)
1989+
ticks_to_draw = axx.xaxis._update_ticks()
19831990
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
19841991
bboxes.extend(tlb)
19851992
bboxes2.extend(tlb2)
@@ -2320,7 +2327,7 @@ def _get_tick_boxes_siblings(self, renderer):
23202327
grp = self.figure._align_ylabel_grp
23212328
# if we want to align labels from other axes:
23222329
for axx in grp.get_siblings(self.axes):
2323-
ticks_to_draw = axx.yaxis._update_ticks(renderer)
2330+
ticks_to_draw = axx.yaxis._update_ticks()
23242331
tlb, tlb2 = axx.yaxis._get_tick_bboxes(ticks_to_draw, renderer)
23252332
bboxes.extend(tlb)
23262333
bboxes2.extend(tlb2)

lib/matplotlib/tests/test_ticker.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,15 @@ def test_offset_value(self, left, right, offset):
324324
UserWarning)
325325
ax.set_xlim(left, right)
326326
assert len(w) == (1 if left == right else 0)
327-
# Update ticks.
328-
next(ax.get_xaxis().iter_ticks())
327+
ax.get_xaxis()._update_ticks()
329328
assert formatter.offset == offset
330329

331330
with warnings.catch_warnings(record=True) as w:
332331
warnings.filterwarnings('always', 'Attempting to set identical',
333332
UserWarning)
334333
ax.set_xlim(right, left)
335334
assert len(w) == (1 if left == right else 0)
336-
# Update ticks.
337-
next(ax.get_xaxis().iter_ticks())
335+
ax.get_xaxis()._update_ticks()
338336
assert formatter.offset == offset
339337

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

lib/mpl_toolkits/mplot3d/axis3d.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -223,25 +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
240-
locmin <= loc <= locmax]
241-
self.major.formatter.set_locs(majorLocs)
242-
majorLabels = [self.major.formatter(val, i)
243-
for i, val in enumerate(majorLocs)]
244-
245231
mins, maxs, centers, deltas, tc, highs = self._get_coord_info(renderer)
246232

247233
# Determine grid lines
@@ -262,9 +248,9 @@ def draw(self, renderer):
262248

263249
# Grid points where the planes meet
264250
xyz0 = []
265-
for val in majorLocs:
251+
for tick in ticks:
266252
coord = minmax.copy()
267-
coord[index] = val
253+
coord[index] = tick.get_loc()
268254
xyz0.append(coord)
269255

270256
# Draw labels
@@ -376,14 +362,14 @@ def draw(self, renderer):
376362
xyz1 = copy.deepcopy(xyz0)
377363
newindex = (index + 1) % 3
378364
newval = get_flip_min_max(xyz1[0], newindex, mins, maxs)
379-
for i in range(len(majorLocs)):
365+
for i in range(len(ticks)):
380366
xyz1[i][newindex] = newval
381367

382368
# Grid points at end of the other plane
383369
xyz2 = copy.deepcopy(xyz0)
384370
newindex = (index + 2) % 3
385371
newval = get_flip_min_max(xyz2[0], newindex, mins, maxs)
386-
for i in range(len(majorLocs)):
372+
for i in range(len(ticks)):
387373
xyz2[i][newindex] = newval
388374

389375
lines = list(zip(xyz1, xyz0, xyz2))
@@ -404,13 +390,11 @@ def draw(self, renderer):
404390
else:
405391
ticksign = -1
406392

407-
for tick, loc, label in zip(majorTicks, majorLocs, majorLabels):
408-
if tick is None:
409-
continue
393+
for tick in ticks:
410394

411395
# Get tick line positions
412396
pos = copy.copy(edgep1)
413-
pos[index] = loc
397+
pos[index] = tick.get_loc()
414398
pos[tickdir] = (
415399
edgep1[tickdir]
416400
+ info['tick']['outward_factor'] * ticksign * tickdelta)
@@ -437,8 +421,6 @@ def draw(self, renderer):
437421
tick_update_position(tick, (x1, x2), (y1, y2), (lx, ly))
438422
tick.tick1line.set_linewidth(info['tick']['linewidth'])
439423
tick.tick1line.set_color(info['tick']['color'])
440-
tick.set_label1(label)
441-
tick.set_label2(label)
442424
tick.draw(renderer)
443425

444426
renderer.close_group('axis3d')

0 commit comments

Comments
 (0)