Skip to content

Commit 4323126

Browse files
Code review comments
1 parent 629d396 commit 4323126

4 files changed

Lines changed: 16 additions & 25 deletions

File tree

lib/matplotlib/axis.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,15 @@ def limit_range_for_scale(self, vmin, vmax):
871871
"""
872872
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())
873873

874+
def _nan_out_of_scale_range(self, data):
875+
"""
876+
Return *data* with values that are out of range for this axis's scale
877+
replaced by NaN. E.g. ``<=0`` on a log axis.
878+
"""
879+
data = np.asanyarray(data, dtype=float)
880+
valid = self._scale.val_in_range(data)
881+
return data if np.all(valid) else np.where(valid, data, np.nan)
882+
874883
def _get_autoscale_on(self):
875884
"""Return whether this Axis is autoscaled."""
876885
return self._autoscale_on

lib/matplotlib/tests/test_scale.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,22 +481,18 @@ def test_val_in_range_base_fallback():
481481

482482
def test_val_in_range_array():
483483
# Vectorized: scalar in -> scalar bool, array in -> bool array.
484-
arr = np.array([1.0, -1.0, 0.0, np.nan, np.inf, 5.0])
484+
arr = np.array([0.5, -1.0, 0.0, np.nan, np.inf, 0.25])
485485
cases = {
486486
'linear': [True, True, True, False, False, True],
487487
'log': [True, False, False, False, False, True],
488488
'symlog': [True, True, True, False, False, True],
489489
'asinh': [True, True, True, False, False, True],
490+
'logit': [True, False, False, False, False, True],
490491
}
491492
for name, expected in cases.items():
492493
s = mscale._scale_mapping[name](axis=None)
493494
np.testing.assert_array_equal(s.val_in_range(arr), expected)
494495

495-
s = mscale._scale_mapping['logit'](axis=None)
496-
np.testing.assert_array_equal(
497-
s.val_in_range(np.array([0.1, 0.5, 0.0, 1.0, -0.1, 1.1])),
498-
[True, True, False, False, False, False])
499-
500496
# 2D shape is preserved.
501497
out = mscale._scale_mapping['log'](axis=None).val_in_range(
502498
np.array([[1.0, -1.0], [0.5, np.nan]]))

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def set_3d_properties(self, z=0, zdir='z', axlim_clip=False):
217217
def draw(self, renderer):
218218
mask = _scale_invalid_mask(self._x, self._y, self._z, self.axes)
219219
if self._axlim_clip:
220-
mask = mask | _viewlim_mask(self._x, self._y, self._z, self.axes)
220+
mask |= _viewlim_mask(self._x, self._y, self._z, self.axes)
221221
if np.any(mask):
222222
pos3d = np.ma.array([self._x, self._y, self._z],
223223
mask=mask, dtype=float).filled(np.nan)
@@ -356,7 +356,7 @@ def get_data_3d(self):
356356
def draw(self, renderer):
357357
scale_mask = _scale_invalid_mask(*self._verts3d, self.axes)
358358
if self._axlim_clip:
359-
scale_mask = scale_mask | _viewlim_mask(*self._verts3d, self.axes)
359+
scale_mask |= _viewlim_mask(*self._verts3d, self.axes)
360360
if np.any(scale_mask):
361361
mask = np.broadcast_to(
362362
scale_mask,

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,6 @@
5858
from . import axis3d
5959

6060

61-
def _mask_scale_invalid(data, axis):
62-
"""
63-
Return ``data`` with values invalid for ``axis``'s scale (e.g. ``<=0`` on a
64-
log axis) replaced by NaN, so they don't pollute data limits.
65-
"""
66-
if data is None:
67-
return data
68-
data = np.asanyarray(data, dtype=float)
69-
valid = axis._scale.val_in_range(data)
70-
if np.all(valid):
71-
return data
72-
return np.where(valid, data, np.nan)
73-
74-
7561
@_docstring.interpd
7662
@_api.define_aliases({
7763
"xlim": ["xlim3d"], "ylim": ["ylim3d"], "zlim": ["zlim3d"]})
@@ -654,16 +640,16 @@ def autoscale(self, enable=True, axis='both', tight=None):
654640
def auto_scale_xyz(self, X, Y, Z=None, had_data=None):
655641
# This updates the bounding boxes as to keep a record as to what the
656642
# minimum sized rectangular volume holds the data.
657-
X = _mask_scale_invalid(X, self.xaxis)
658-
Y = _mask_scale_invalid(Y, self.yaxis)
643+
X = self.xaxis._nan_out_of_scale_range(X)
644+
Y = self.yaxis._nan_out_of_scale_range(Y)
659645
if np.shape(X) == np.shape(Y):
660646
self.xy_dataLim.update_from_data_xy(
661647
np.column_stack([np.ravel(X), np.ravel(Y)]), not had_data)
662648
else:
663649
self.xy_dataLim.update_from_data_x(X, not had_data)
664650
self.xy_dataLim.update_from_data_y(Y, not had_data)
665651
if Z is not None:
666-
Z = _mask_scale_invalid(Z, self.zaxis)
652+
Z = self.zaxis._nan_out_of_scale_range(Z)
667653
self.zz_dataLim.update_from_data_x(Z, not had_data)
668654
# Let autoscale_view figure out how to use this data.
669655
self.autoscale_view()

0 commit comments

Comments
 (0)