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

Skip to content

Commit eeda1d1

Browse files
committed
MNT: simplify valid tick logic
1 parent a43fd85 commit eeda1d1

10 files changed

+22
-48
lines changed

lib/matplotlib/axis.py

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,60 +1030,28 @@ def _update_ticks(self, renderer):
10301030
ihigh = locs[-1]
10311031
tick_tups = [ti for ti in tick_tups if ilow <= ti[1] <= ihigh]
10321032

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

10781037
ticks_to_draw = []
10791038
for tick, loc, label in tick_tups:
1039+
# draw each tick if it is in interval. Note the transform
1040+
# to pixel space to take care of log transforms etc.
1041+
# interval_contains has a floating point tolerance.
10801042
if tick is None:
10811043
continue
10821044
# NB: always update labels and position to avoid issues like #9397
10831045
tick.update_position(loc)
10841046
tick.set_label1(label)
10851047
tick.set_label2(label)
1086-
if not mtransforms.interval_contains(interval_expanded, loc):
1048+
try:
1049+
loct = self.get_transform().transform(loc)
1050+
except AssertionError:
1051+
loct = None
1052+
continue
1053+
if ((loct is None) or
1054+
(not mtransforms.interval_contains(inter, loct))):
10871055
continue
10881056
ticks_to_draw.append(tick)
10891057

lib/matplotlib/transforms.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
28922892
return vmin, vmax
28932893

28942894

2895-
def interval_contains(interval, val):
2895+
def interval_contains(interval, val, rtol=1e-10):
28962896
"""
28972897
Check, inclusively, whether an interval includes a given value.
28982898
@@ -2902,14 +2902,20 @@ def interval_contains(interval, val):
29022902
A 2-length sequence, endpoints that define the interval.
29032903
val : scalar
29042904
Value to check is within interval.
2905+
rtol : scalar
2906+
Tolerance slippage allowed outside of this interval. Default
2907+
1e-10 * (b - a).
29052908
29062909
Returns
29072910
-------
29082911
bool
2909-
Returns true if given val is within the interval.
2912+
Returns true if given val is within the interval (with tolerance)
29102913
"""
29112914
a, b = interval
2912-
return a <= val <= b or a >= val >= b
2915+
if a > b:
2916+
a, b = b, a
2917+
rtol = (b - a) * rtol
2918+
return a - rtol <= val <= b + rtol
29132919

29142920

29152921
def interval_contains_open(interval, val):

0 commit comments

Comments
 (0)