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

Skip to content

Inline iter_ticks into _update_ticks, and use that in mplot3d. #13363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions doc/api/axis_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ Ticks, tick labels and Offset text
Axis.get_gridlines
Axis.grid

Axis.iter_ticks
Axis.set_tick_params

Axis.axis_date
Expand Down Expand Up @@ -365,7 +364,6 @@ YAxis
YAxis.get_units
YAxis.get_view_interval
YAxis.grid
YAxis.iter_ticks
YAxis.limit_range_for_scale
YAxis.pan
YAxis.reset_ticks
Expand Down Expand Up @@ -432,7 +430,6 @@ XAxis
XAxis.get_units
XAxis.get_view_interval
XAxis.grid
XAxis.iter_ticks
XAxis.limit_range_for_scale
XAxis.pan
XAxis.reset_ticks
Expand Down
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/2018-02-05-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Changes to the internal tick handling API
`````````````````````````````````````````

``Axis.iter_ticks`` (which only served as a helper to the private
``Axis._update_ticks``) is deprecated.

The signature of the (private) ``Axis._update_ticks`` has been changed to not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axis_artist.AxisArtist._update_ticks() also takes a renderer argument; and it uses it. Not quite clear if we want to keep the APIs similar in both worlds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't even return the same kind of things (one returns a list of ticks, the other returns a bounding box) so I wouldn't worry about that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I approve.

take the renderer as argument anymore (that argument is unused).
75 changes: 41 additions & 34 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ def _set_artist_props(self, a):
return
a.set_figure(self.figure)

@cbook.deprecated("3.1")
def iter_ticks(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, but I wonder if you want a private _locate_ticks and then call it here, and in the _update_ticks. That way the (deprecated) public function doesn't drift from the private implimentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then (hypothetical, but that's what this discussion is about) changes in _locate_ticks would be reflected in iter_ticks, and you'd need to add additional changelog entries for changes in the behavior of the deprecated method, which seems a bit silly.

"""
Yield ``(Tick, location, label)`` tuples for major and minor ticks.
Expand All @@ -1035,7 +1036,7 @@ def get_ticklabel_extents(self, renderer):
of the axes.
"""

ticks_to_draw = self._update_ticks(renderer)
ticks_to_draw = self._update_ticks()
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
renderer)

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

def _update_ticks(self, renderer):
def _update_ticks(self):
"""
Update ticks (position and labels) using the current data
interval of the axes. Returns a list of ticks that will be
drawn.
Update ticks (position and labels) using the current data interval of
the axes. Return the list of ticks that will be drawn.
"""

interval = self.get_view_interval()
tick_tups = list(self.iter_ticks()) # iter_ticks calls the locator
if self._smart_bounds and tick_tups:
major_locs = self.major.locator()
major_ticks = self.get_major_ticks(len(major_locs))
self.major.formatter.set_locs(major_locs)
major_labels = self.major.formatter.format_ticks(major_locs)
for tick, loc, label in zip(major_ticks, major_locs, major_labels):
tick.update_position(loc)
tick.set_label1(label)
tick.set_label2(label)
minor_locs = self.minor.locator()
minor_ticks = self.get_minor_ticks(len(minor_locs))
self.minor.formatter.set_locs(minor_locs)
minor_labels = self.minor.formatter.format_ticks(minor_locs)
for tick, loc, label in zip(minor_ticks, minor_locs, minor_labels):
tick.update_position(loc)
tick.set_label1(label)
tick.set_label2(label)
ticks = [*major_ticks, *minor_ticks]

view_low, view_high = self.get_view_interval()
if view_low > view_high:
view_low, view_high = view_high, view_low

if self._smart_bounds and ticks:
# handle inverted limits
view_low, view_high = sorted(interval)
data_low, data_high = sorted(self.get_data_interval())
locs = np.sort([ti[1] for ti in tick_tups])
locs = np.sort([tick.get_loc() for tick in ticks])
if data_low <= view_low:
# data extends beyond view, take view as limit
ilow = view_low
Expand All @@ -1096,33 +1115,21 @@ def _update_ticks(self, renderer):
else:
# No ticks (why not?), take last tick
ihigh = locs[-1]
tick_tups = [ti for ti in tick_tups if ilow <= ti[1] <= ihigh]
ticks = [tick for tick in ticks if ilow <= tick.get_loc() <= ihigh]

if interval[1] <= interval[0]:
interval = interval[1], interval[0]
inter = self.get_transform().transform(interval)
interval_t = self.get_transform().transform([view_low, view_high])

ticks_to_draw = []
for tick, loc, label in tick_tups:
# draw each tick if it is in interval. Note the transform
# to pixel space to take care of log transforms etc.
# interval_contains has a floating point tolerance.
if tick is None:
continue
# NB: always update labels and position to avoid issues like #9397
tick.update_position(loc)
tick.set_label1(label)
tick.set_label2(label)
for tick in ticks:
try:
loct = self.get_transform().transform(loc)
loc_t = self.get_transform().transform(tick.get_loc())
except AssertionError:
# transforms.transform doesn't allow masked values but
# some scales might make them, so we need this try/except.
loct = None
continue
if not mtransforms._interval_contains_close(inter, loct):
continue
ticks_to_draw.append(tick)
pass
else:
if mtransforms._interval_contains_close(interval_t, loc_t):
ticks_to_draw.append(tick)

return ticks_to_draw

Expand All @@ -1141,7 +1148,7 @@ def get_tightbbox(self, renderer):
if not self.get_visible():
return

ticks_to_draw = self._update_ticks(renderer)
ticks_to_draw = self._update_ticks()

self._update_label_position(renderer)

Expand Down Expand Up @@ -1182,7 +1189,7 @@ def draw(self, renderer, *args, **kwargs):
return
renderer.open_group(__name__)

ticks_to_draw = self._update_ticks(renderer)
ticks_to_draw = self._update_ticks()
ticklabelBoxes, ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,
renderer)

Expand Down Expand Up @@ -1948,7 +1955,7 @@ def _get_tick_boxes_siblings(self, renderer):
grp = self.figure._align_xlabel_grp
# if we want to align labels from other axes:
for nn, axx in enumerate(grp.get_siblings(self.axes)):
ticks_to_draw = axx.xaxis._update_ticks(renderer)
ticks_to_draw = axx.xaxis._update_ticks()
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
bboxes.extend(tlb)
bboxes2.extend(tlb2)
Expand Down Expand Up @@ -2262,7 +2269,7 @@ def _get_tick_boxes_siblings(self, renderer):
grp = self.figure._align_ylabel_grp
# if we want to align labels from other axes:
for axx in grp.get_siblings(self.axes):
ticks_to_draw = axx.yaxis._update_ticks(renderer)
ticks_to_draw = axx.yaxis._update_ticks()
tlb, tlb2 = axx.yaxis._get_tick_bboxes(ticks_to_draw, renderer)
bboxes.extend(tlb)
bboxes2.extend(tlb2)
Expand Down
Binary file not shown.
18 changes: 9 additions & 9 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ def test_inverted_cla():
plt.close(fig)


@image_comparison(baseline_images=["minorticks_on_rcParams_both"],
extensions=['png'])
def test_minorticks_on_rcParams_both():
fig = plt.figure()
matplotlib.rcParams['xtick.minor.visible'] = True
matplotlib.rcParams['ytick.minor.visible'] = True

plt.plot([0, 1], [0, 1])
plt.axis([0, 1, 0, 1])
@check_figures_equal(extensions=["png"])
def test_minorticks_on_rcParams_both(fig_test, fig_ref):
with matplotlib.rc_context({"xtick.minor.visible": True,
"ytick.minor.visible": True}):
ax_test = fig_test.subplots()
ax_test.plot([0, 1], [0, 1])
ax_ref = fig_ref.subplots()
ax_ref.plot([0, 1], [0, 1])
ax_ref.minorticks_on()


@image_comparison(baseline_images=["autoscale_tiny_range"], remove_text=True)
Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,15 @@ def test_offset_value(self, left, right, offset):
UserWarning)
ax.set_xlim(left, right)
assert len(w) == (1 if left == right else 0)
# Update ticks.
next(ax.get_xaxis().iter_ticks())
ax.get_xaxis()._update_ticks()
assert formatter.offset == offset

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', 'Attempting to set identical',
UserWarning)
ax.set_xlim(right, left)
assert len(w) == (1 if left == right else 0)
# Update ticks.
next(ax.get_xaxis().iter_ticks())
ax.get_xaxis()._update_ticks()
assert formatter.offset == offset

@pytest.mark.parametrize('use_offset', use_offset_data)
Expand Down
29 changes: 7 additions & 22 deletions lib/mpl_toolkits/mplot3d/axis3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,11 @@ def draw(self, renderer):
self.label._transform = self.axes.transData
renderer.open_group('axis3d')

# code from XAxis
majorTicks = self.get_major_ticks()
majorLocs = self.major.locator()
ticks = self._update_ticks()

info = self._axinfo
index = info['i']

# filter locations here so that no extra grid lines are drawn
locmin, locmax = self.get_view_interval()
if locmin > locmax:
locmin, locmax = locmax, locmin

# Rudimentary clipping
majorLocs = [loc for loc in majorLocs if locmin <= loc <= locmax]
majorLabels = self.major.formatter.format_ticks(majorLocs)

mins, maxs, centers, deltas, tc, highs = self._get_coord_info(renderer)

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

# Grid points where the planes meet
xyz0 = []
for val in majorLocs:
for tick in ticks:
coord = minmax.copy()
coord[index] = val
coord[index] = tick.get_loc()
xyz0.append(coord)

# Draw labels
Expand Down Expand Up @@ -373,14 +362,14 @@ def draw(self, renderer):
xyz1 = copy.deepcopy(xyz0)
newindex = (index + 1) % 3
newval = get_flip_min_max(xyz1[0], newindex, mins, maxs)
for i in range(len(majorLocs)):
for i in range(len(ticks)):
xyz1[i][newindex] = newval

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

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

for tick, loc, label in zip(majorTicks, majorLocs, majorLabels):
if tick is None:
continue
for tick in ticks:

# Get tick line positions
pos = copy.copy(edgep1)
pos[index] = loc
pos[index] = tick.get_loc()
pos[tickdir] = (
edgep1[tickdir]
+ info['tick']['outward_factor'] * ticksign * tickdelta)
Expand All @@ -434,8 +421,6 @@ def draw(self, renderer):
tick_update_position(tick, (x1, x2), (y1, y2), (lx, ly))
tick.tick1line.set_linewidth(info['tick']['linewidth'])
tick.tick1line.set_color(info['tick']['color'])
tick.set_label1(label)
tick.set_label2(label)
tick.draw(renderer)

renderer.close_group('axis3d')
Expand Down