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

Skip to content

Commit 227bed5

Browse files
committed
MNT: simplify valid tick logic
1 parent 02d7f38 commit 227bed5

10 files changed

+45
-49
lines changed

lib/matplotlib/axis.py

Lines 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

lib/matplotlib/transforms.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,10 +2906,39 @@ def interval_contains(interval, val):
29062906
Returns
29072907
-------
29082908
bool
2909-
Returns true if given val is within the interval.
2909+
Returns *True* if given *val* is within the *interval*.
2910+
"""
2911+
a, b = interval
2912+
if a > b:
2913+
a, b = b, a
2914+
return a <= val <= b
2915+
2916+
2917+
def interval_contains_close(interval, val, rtol=1e-10):
2918+
"""
2919+
Check, inclusively, whether an interval includes a given value, with the
2920+
interval expanded by a small tolerance to admit floating point errors.
2921+
2922+
Parameters
2923+
----------
2924+
interval : sequence of scalar
2925+
A 2-length sequence, endpoints that define the interval.
2926+
val : scalar
2927+
Value to check is within interval.
2928+
rtol : scalar
2929+
Tolerance slippage allowed outside of this interval. Default
2930+
1e-10 * (b - a).
2931+
2932+
Returns
2933+
-------
2934+
bool
2935+
Returns *True* if given *val* is within the *interval* (with tolerance)
29102936
"""
29112937
a, b = interval
2912-
return a <= val <= b or a >= val >= b
2938+
if a > b:
2939+
a, b = b, a
2940+
rtol = (b - a) * rtol
2941+
return a - rtol <= val <= b + rtol
29132942

29142943

29152944
def interval_contains_open(interval, val):

0 commit comments

Comments
 (0)