Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 01d7455

Browse filesBrowse files
committed
MNT: simplify valid tick logic
FIX: make contains_close private
1 parent a7f129c commit 01d7455
Copy full SHA for 01d7455
Expand file treeCollapse file tree

10 files changed

+45
-49
lines changed

‎lib/matplotlib/axis.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axis.py
+14-47Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,62 +1028,29 @@ def _update_ticks(self, renderer):
10281028
ihigh = locs[-1]
10291029
tick_tups = [ti for ti in tick_tups if ilow <= ti[1] <= ihigh]
10301030

1031-
# so that we don't lose ticks on the end, expand out the interval ever
1032-
# so slightly. The "ever so slightly" is defined to be the width of a
1033-
# half of a pixel. We don't want to draw a tick that even one pixel
1034-
# outside of the defined axis interval.
1035-
if interval[0] <= interval[1]:
1036-
interval_expanded = interval
1037-
else:
1038-
interval_expanded = interval[1], interval[0]
1039-
1040-
if hasattr(self, '_get_pixel_distance_along_axis'):
1041-
# normally, one does not want to catch all exceptions that
1042-
# could possibly happen, but it is not clear exactly what
1043-
# exceptions might arise from a user's projection (their
1044-
# rendition of the Axis object). So, we catch all, with
1045-
# the idea that one would rather potentially lose a tick
1046-
# from one side of the axis or another, rather than see a
1047-
# stack trace.
1048-
# We also catch users warnings here. These are the result of
1049-
# invalid numpy calculations that may be the result of out of
1050-
# bounds on axis with finite allowed intervals such as geo
1051-
# projections i.e. Mollweide.
1052-
with np.errstate(invalid='ignore'):
1053-
try:
1054-
ds1 = self._get_pixel_distance_along_axis(
1055-
interval_expanded[0], -0.5)
1056-
except Exception:
1057-
cbook._warn_external("Unable to find pixel distance "
1058-
"along axis for interval padding of "
1059-
"ticks; assuming no interval "
1060-
"padding needed.")
1061-
ds1 = 0.0
1062-
if np.isnan(ds1):
1063-
ds1 = 0.0
1064-
try:
1065-
ds2 = self._get_pixel_distance_along_axis(
1066-
interval_expanded[1], +0.5)
1067-
except Exception:
1068-
cbook._warn_external("Unable to find pixel distance "
1069-
"along axis for interval padding of "
1070-
"ticks; assuming no interval "
1071-
"padding needed.")
1072-
ds2 = 0.0
1073-
if np.isnan(ds2):
1074-
ds2 = 0.0
1075-
interval_expanded = (interval_expanded[0] - ds1,
1076-
interval_expanded[1] + ds2)
1031+
if interval[1] <= interval[0]:
1032+
interval = interval[1], interval[0]
1033+
inter = self.get_transform().transform(interval)
10771034

10781035
ticks_to_draw = []
10791036
for tick, loc, label in tick_tups:
1037+
# draw each tick if it is in interval. Note the transform
1038+
# to pixel space to take care of log transforms etc.
1039+
# interval_contains has a floating point tolerance.
10801040
if tick is None:
10811041
continue
10821042
# NB: always update labels and position to avoid issues like #9397
10831043
tick.update_position(loc)
10841044
tick.set_label1(label)
10851045
tick.set_label2(label)
1086-
if not mtransforms.interval_contains(interval_expanded, loc):
1046+
try:
1047+
loct = self.get_transform().transform(loc)
1048+
except AssertionError:
1049+
# transforms.transform doesn't allow masked values but
1050+
# some scales might make them, so we need this try/except.
1051+
loct = None
1052+
continue
1053+
if not mtransforms._interval_contains_close(inter, loct):
10871054
continue
10881055
ticks_to_draw.append(tick)
10891056

Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

‎lib/matplotlib/transforms.py

Copy file name to clipboardExpand all lines: lib/matplotlib/transforms.py
+31-2Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,10 +2900,39 @@ def interval_contains(interval, val):
29002900
Returns
29012901
-------
29022902
bool
2903-
Returns true if given val is within the interval.
2903+
Returns *True* if given *val* is within the *interval*.
2904+
"""
2905+
a, b = interval
2906+
if a > b:
2907+
a, b = b, a
2908+
return a <= val <= b
2909+
2910+
2911+
def _interval_contains_close(interval, val, rtol=1e-10):
2912+
"""
2913+
Check, inclusively, whether an interval includes a given value, with the
2914+
interval expanded by a small tolerance to admit floating point errors.
2915+
2916+
Parameters
2917+
----------
2918+
interval : sequence of scalar
2919+
A 2-length sequence, endpoints that define the interval.
2920+
val : scalar
2921+
Value to check is within interval.
2922+
rtol : scalar
2923+
Tolerance slippage allowed outside of this interval. Default
2924+
1e-10 * (b - a).
2925+
2926+
Returns
2927+
-------
2928+
bool
2929+
Returns *True* if given *val* is within the *interval* (with tolerance)
29042930
"""
29052931
a, b = interval
2906-
return a <= val <= b or a >= val >= b
2932+
if a > b:
2933+
a, b = b, a
2934+
rtol = (b - a) * rtol
2935+
return a - rtol <= val <= b + rtol
29072936

29082937

29092938
def interval_contains_open(interval, val):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.