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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,8 +2479,7 @@ def _point_in_data_domain(self, x, y):
(e.g. negative coordinates with a log scale).
"""
for val, axis in zip([x, y], self._axis_map.values()):
vmin, vmax = axis.limit_range_for_scale(val, val)
if vmin != val or vmax != val:
if not axis._scale.val_in_range(val):
return False
return True

Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,13 @@
current scale.
"""
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())

Check warning on line 827 in lib/matplotlib/axis.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/axis.py" range:{start:{line:827 column:1} end:{line:827 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:827 column:1} end:{line:827 column:5}}}
def val_in_range(self, val):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the motivation to expose this on the Axis?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I noticed limit_range_in_scale being available in Axis class, so exposing the val_in_range seemed consistent to me. But I later forgot to implement the wrapper in _point_in_data_domain.

I have two possible approaches here:

  1. Keep the wrapper exposed in Axis and use it in the _point_in_data_domain.
  2. Remove the wrapper from Axis and keep the current _point_in_data_domain, using the _scale attribute, unchanged.

After going through other exposed apis, I feel removing the wrapper here would be fine as currently there is no other use of val_in_range except in _point_in_data_domain.

"""
Return `True` if the value(s) lie within the domain supported by the

Check warning on line 830 in lib/matplotlib/axis.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/axis.py" range:{start:{line:830 column:77} end:{line:830 column:78}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:830 column:77} end:{line:830 column:78}}}
current scale.
"""
return self._scale.val_in_range(val)

def _get_autoscale_on(self):
"""Return whether this Axis is autoscaled."""
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/axis.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Axis(martist.Artist):
def limit_range_for_scale(
self, vmin: float, vmax: float
) -> tuple[float, float]: ...
def val_in_range(self, val: ArrayLike) -> bool | np.ndarray: ...
def get_children(self) -> list[martist.Artist]: ...
# TODO units
converter: Any
Expand Down
29 changes: 29 additions & 0 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@
This is used by log scales to determine a minimum value.
"""
return vmin, vmax

Check warning on line 116 in lib/matplotlib/scale.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/scale.py" range:{start:{line:116 column:1} end:{line:116 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:116 column:1} end:{line:116 column:5}}}
def val_in_range(self, val):
"""
Return whether the value(s) are within the valid range for this scale.
"""
Comment on lines +120 to +124
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Return whether the value(s) are within the valid range for this scale.
"""
Return whether the value(s) are within the valid range for this scale.
This method is a generic implementation. Subclasses may implement
more efficent solutions for their domain.
"""

if np.isscalar(val):
vmin, vmax = self.limit_range_for_scale(val, val, minpos=1e-300)
return (vmax == val) and (vmin == val)

val = np.asanyarray(val)
return np.array([self.val_in_range(v) for v in val])


def _make_axis_parameter_optional(init_func):
Expand Down Expand Up @@ -195,6 +206,13 @@
`~matplotlib.transforms.IdentityTransform`.
"""
return IdentityTransform()

Check warning on line 209 in lib/matplotlib/scale.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/scale.py" range:{start:{line:209 column:1} end:{line:209 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:209 column:1} end:{line:209 column:5}}}
def val_in_range(self, val):
"""
Return `True` for all values.
"""
Comment on lines +217 to +221
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The docstring should always descripe the concept, not just the imlementation. A docstring

def some_func():
    """Return True."""
    return True

has zero information conent.

Instead do:

Suggested change
"""
Return `True` for all values.
"""
"""
Return whether the value(s) are within the valid range for this scale.
This is True for all inputs.
"""

The first sentence should be in all val_in_range docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was actually pretty enlightening. Kind of changed the way I thought about docstrings.
I'm making the needed changes. Thank you very much for the suggestion.

val = np.asanyarray(val)
return np.ones(val.shape, dtype=bool)


class FuncTransform(Transform):
Expand Down Expand Up @@ -399,6 +417,10 @@

return (minpos if vmin <= 0 else vmin,
minpos if vmax <= 0 else vmax)

Check warning on line 420 in lib/matplotlib/scale.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/scale.py" range:{start:{line:420 column:1} end:{line:420 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:420 column:1} end:{line:420 column:5}}}
def val_in_range(self, val):
"""Return `True` for positive values."""
return np.asanyarray(val) > 0


class FuncScaleLog(LogScale):
Expand Down Expand Up @@ -819,6 +841,13 @@
minpos = 1e-7 # Should rarely (if ever) have a visible effect.
return (minpos if vmin <= 0 else vmin,
1 - minpos if vmax >= 1 else vmax)

Check warning on line 844 in lib/matplotlib/scale.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/scale.py" range:{start:{line:844 column:1} end:{line:844 column:5}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:844 column:1} end:{line:844 column:5}}}
def val_in_range(self, val):
"""
Return `True` if value(s) lie between 0 and 1 (excluded)
"""
val = np.asanyarray(val)
return (val > 0) & (val < 1)


_scale_mapping = {
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/scale.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from matplotlib.transforms import Transform

from collections.abc import Callable, Iterable
from typing import Literal
import numpy as np
from numpy.typing import ArrayLike

class ScaleBase:
Expand All @@ -12,6 +13,7 @@ class ScaleBase:
def limit_range_for_scale(
self, vmin: float, vmax: float, minpos: float
) -> tuple[float, float]: ...
def val_in_range(self, val: ArrayLike) -> bool | np.ndarray: ...

class LinearScale(ScaleBase):
name: str
Expand Down
Loading