Skip to content

Use a default value for constant dimensions #7281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2022
Merged
Changes from all 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
44 changes: 23 additions & 21 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
ROBUST_PERCENTILE = 2.0

# copied from seaborn
_MARKERSIZE_RANGE = (18.0, 72.0)
_LINEWIDTH_RANGE = (1.5, 6.0)
_MARKERSIZE_RANGE = (18.0, 36.0, 72.0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not really a range anymore, but since it is internal only I don't care much.

_LINEWIDTH_RANGE = (1.5, 1.5, 6.0)


def import_matplotlib_pyplot():
Expand Down Expand Up @@ -1341,7 +1341,7 @@ def _parse_size(
else:
levels = numbers = np.sort(np.unique(flatdata))

min_width, max_width = _MARKERSIZE_RANGE
min_width, default_width, max_width = _MARKERSIZE_RANGE
# width_range = min_width, max_width

if norm is None:
Expand Down Expand Up @@ -1378,8 +1378,8 @@ class _Normalize(Sequence):
----------
data : DataArray
DataArray to normalize.
width : Sequence of two numbers, optional
Normalize the data to theses min and max values.
width : Sequence of three numbers, optional
Normalize the data to these (min, default, max) values.
The default is None.
"""

Expand All @@ -1388,7 +1388,7 @@ class _Normalize(Sequence):
_data_unique_index: np.ndarray
_data_unique_inverse: np.ndarray
_data_is_numeric: bool
_width: tuple[float, float] | None
_width: tuple[float, float, float] | None

__slots__ = (
"_data",
Expand All @@ -1402,7 +1402,7 @@ class _Normalize(Sequence):
def __init__(
self,
data: DataArray | None,
width: tuple[float, float] | None = None,
width: tuple[float, float, float] | None = None,
_is_facetgrid: bool = False,
) -> None:
self._data = data
Expand Down Expand Up @@ -1469,14 +1469,16 @@ def _calc_widths(self, y: np.ndarray | DataArray) -> np.ndarray | DataArray:
if self._width is None:
return y

x0, x1 = self._width
xmin, xdefault, xmax = self._width

# If y is constant, then add a small number to avoid division with zero:
diff_maxy_miny = np.max(y) - np.min(y)
eps = np.finfo(np.float64).eps if diff_maxy_miny == 0 else 0
k = (y - np.min(y)) / (diff_maxy_miny + eps)
widths = x0 + k * (x1 - x0)

if diff_maxy_miny == 0:
# Use default with if y is constant:
widths = xdefault + 0 * y
else:
# Normalize inbetween xmin and xmax:
k = (y - np.min(y)) / diff_maxy_miny
widths = xmin + k * (xmax - xmin)
return widths

@overload
Expand Down Expand Up @@ -1507,7 +1509,7 @@ def values(self) -> DataArray | None:
array([3, 1, 1, 3, 5])
Dimensions without coordinates: dim_0

>>> _Normalize(a, width=[18, 72]).values
>>> _Normalize(a, width=(18, 36, 72)).values
<xarray.DataArray (dim_0: 5)>
array([45., 18., 18., 45., 72.])
Dimensions without coordinates: dim_0
Expand All @@ -1518,14 +1520,14 @@ def values(self) -> DataArray | None:
array([0.5, 0. , 0. , 0.5, 2. , 3. ])
Dimensions without coordinates: dim_0

>>> _Normalize(a, width=[18, 72]).values
>>> _Normalize(a, width=(18, 36, 72)).values
<xarray.DataArray (dim_0: 6)>
array([27., 18., 18., 27., 54., 72.])
Dimensions without coordinates: dim_0

>>> _Normalize(a * 0, width=[18, 72]).values
>>> _Normalize(a * 0, width=(18, 36, 72)).values
<xarray.DataArray (dim_0: 6)>
array([18., 18., 18., 18., 18., 18.])
array([36., 36., 36., 36., 36., 36.])
Dimensions without coordinates: dim_0

"""
Expand All @@ -1552,14 +1554,14 @@ def _values_unique(self) -> np.ndarray | None:
>>> _Normalize(a)._values_unique
array([1, 3, 5])

>>> _Normalize(a, width=[18, 72])._values_unique
>>> _Normalize(a, width=(18, 36, 72))._values_unique
array([18., 45., 72.])

>>> a = xr.DataArray([0.5, 0, 0, 0.5, 2, 3])
>>> _Normalize(a)._values_unique
array([0. , 0.5, 2. , 3. ])

>>> _Normalize(a, width=[18, 72])._values_unique
>>> _Normalize(a, width=(18, 36, 72))._values_unique
array([18., 27., 54., 72.])
"""
if self.data is None:
Expand Down Expand Up @@ -1631,7 +1633,7 @@ def format(self) -> FuncFormatter:
Examples
--------
>>> a = xr.DataArray([0.5, 0, 0, 0.5, 2, 3])
>>> aa = _Normalize(a, width=[0, 1])
>>> aa = _Normalize(a, width=(0, 0.5, 1))
>>> aa._lookup
0.000000 0.0
0.166667 0.5
Expand All @@ -1657,7 +1659,7 @@ def func(self) -> Callable[[Any, None | Any], Any]:
Examples
--------
>>> a = xr.DataArray([0.5, 0, 0, 0.5, 2, 3])
>>> aa = _Normalize(a, width=[0, 1])
>>> aa = _Normalize(a, width=(0, 0.5, 1))
>>> aa._lookup
0.000000 0.0
0.166667 0.5
Expand Down