Skip to content

Commit 9dd3c1b

Browse files
authored
Use a default value for constant dimensions (#7281)
* Add a default value for constant dimensions * Update utils.py
1 parent 63a69fe commit 9dd3c1b

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

xarray/plot/utils.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
ROBUST_PERCENTILE = 2.0
5252

5353
# copied from seaborn
54-
_MARKERSIZE_RANGE = (18.0, 72.0)
55-
_LINEWIDTH_RANGE = (1.5, 6.0)
54+
_MARKERSIZE_RANGE = (18.0, 36.0, 72.0)
55+
_LINEWIDTH_RANGE = (1.5, 1.5, 6.0)
5656

5757

5858
def import_matplotlib_pyplot():
@@ -1341,7 +1341,7 @@ def _parse_size(
13411341
else:
13421342
levels = numbers = np.sort(np.unique(flatdata))
13431343

1344-
min_width, max_width = _MARKERSIZE_RANGE
1344+
min_width, default_width, max_width = _MARKERSIZE_RANGE
13451345
# width_range = min_width, max_width
13461346

13471347
if norm is None:
@@ -1378,8 +1378,8 @@ class _Normalize(Sequence):
13781378
----------
13791379
data : DataArray
13801380
DataArray to normalize.
1381-
width : Sequence of two numbers, optional
1382-
Normalize the data to theses min and max values.
1381+
width : Sequence of three numbers, optional
1382+
Normalize the data to these (min, default, max) values.
13831383
The default is None.
13841384
"""
13851385

@@ -1388,7 +1388,7 @@ class _Normalize(Sequence):
13881388
_data_unique_index: np.ndarray
13891389
_data_unique_inverse: np.ndarray
13901390
_data_is_numeric: bool
1391-
_width: tuple[float, float] | None
1391+
_width: tuple[float, float, float] | None
13921392

13931393
__slots__ = (
13941394
"_data",
@@ -1402,7 +1402,7 @@ class _Normalize(Sequence):
14021402
def __init__(
14031403
self,
14041404
data: DataArray | None,
1405-
width: tuple[float, float] | None = None,
1405+
width: tuple[float, float, float] | None = None,
14061406
_is_facetgrid: bool = False,
14071407
) -> None:
14081408
self._data = data
@@ -1469,14 +1469,16 @@ def _calc_widths(self, y: np.ndarray | DataArray) -> np.ndarray | DataArray:
14691469
if self._width is None:
14701470
return y
14711471

1472-
x0, x1 = self._width
1472+
xmin, xdefault, xmax = self._width
14731473

1474-
# If y is constant, then add a small number to avoid division with zero:
14751474
diff_maxy_miny = np.max(y) - np.min(y)
1476-
eps = np.finfo(np.float64).eps if diff_maxy_miny == 0 else 0
1477-
k = (y - np.min(y)) / (diff_maxy_miny + eps)
1478-
widths = x0 + k * (x1 - x0)
1479-
1475+
if diff_maxy_miny == 0:
1476+
# Use default with if y is constant:
1477+
widths = xdefault + 0 * y
1478+
else:
1479+
# Normalize inbetween xmin and xmax:
1480+
k = (y - np.min(y)) / diff_maxy_miny
1481+
widths = xmin + k * (xmax - xmin)
14801482
return widths
14811483

14821484
@overload
@@ -1507,7 +1509,7 @@ def values(self) -> DataArray | None:
15071509
array([3, 1, 1, 3, 5])
15081510
Dimensions without coordinates: dim_0
15091511
1510-
>>> _Normalize(a, width=[18, 72]).values
1512+
>>> _Normalize(a, width=(18, 36, 72)).values
15111513
<xarray.DataArray (dim_0: 5)>
15121514
array([45., 18., 18., 45., 72.])
15131515
Dimensions without coordinates: dim_0
@@ -1518,14 +1520,14 @@ def values(self) -> DataArray | None:
15181520
array([0.5, 0. , 0. , 0.5, 2. , 3. ])
15191521
Dimensions without coordinates: dim_0
15201522
1521-
>>> _Normalize(a, width=[18, 72]).values
1523+
>>> _Normalize(a, width=(18, 36, 72)).values
15221524
<xarray.DataArray (dim_0: 6)>
15231525
array([27., 18., 18., 27., 54., 72.])
15241526
Dimensions without coordinates: dim_0
15251527
1526-
>>> _Normalize(a * 0, width=[18, 72]).values
1528+
>>> _Normalize(a * 0, width=(18, 36, 72)).values
15271529
<xarray.DataArray (dim_0: 6)>
1528-
array([18., 18., 18., 18., 18., 18.])
1530+
array([36., 36., 36., 36., 36., 36.])
15291531
Dimensions without coordinates: dim_0
15301532
15311533
"""
@@ -1552,14 +1554,14 @@ def _values_unique(self) -> np.ndarray | None:
15521554
>>> _Normalize(a)._values_unique
15531555
array([1, 3, 5])
15541556
1555-
>>> _Normalize(a, width=[18, 72])._values_unique
1557+
>>> _Normalize(a, width=(18, 36, 72))._values_unique
15561558
array([18., 45., 72.])
15571559
15581560
>>> a = xr.DataArray([0.5, 0, 0, 0.5, 2, 3])
15591561
>>> _Normalize(a)._values_unique
15601562
array([0. , 0.5, 2. , 3. ])
15611563
1562-
>>> _Normalize(a, width=[18, 72])._values_unique
1564+
>>> _Normalize(a, width=(18, 36, 72))._values_unique
15631565
array([18., 27., 54., 72.])
15641566
"""
15651567
if self.data is None:
@@ -1631,7 +1633,7 @@ def format(self) -> FuncFormatter:
16311633
Examples
16321634
--------
16331635
>>> a = xr.DataArray([0.5, 0, 0, 0.5, 2, 3])
1634-
>>> aa = _Normalize(a, width=[0, 1])
1636+
>>> aa = _Normalize(a, width=(0, 0.5, 1))
16351637
>>> aa._lookup
16361638
0.000000 0.0
16371639
0.166667 0.5
@@ -1657,7 +1659,7 @@ def func(self) -> Callable[[Any, None | Any], Any]:
16571659
Examples
16581660
--------
16591661
>>> a = xr.DataArray([0.5, 0, 0, 0.5, 2, 3])
1660-
>>> aa = _Normalize(a, width=[0, 1])
1662+
>>> aa = _Normalize(a, width=(0, 0.5, 1))
16611663
>>> aa._lookup
16621664
0.000000 0.0
16631665
0.166667 0.5

0 commit comments

Comments
 (0)