Skip to content

Commit f9ac77f

Browse files
committed
Skip auto reverse axis for weird dependent coords
1 parent fe79211 commit f9ac77f

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

proplot/axes/plot.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,30 +1911,22 @@ def _parse_1d_format(
19111911
if axis.isDefault_label:
19121912
kw_format[sy + 'label'] = title
19131913

1914-
# Convert string-type coordinates
1914+
# Convert string-type coordinates to indices
19151915
# NOTE: This should even allow qualitative string input to hist()
19161916
if autox:
19171917
x, kw_format = inputs._meta_coords(x, which=sx, **kw_format)
19181918
if autoy:
19191919
*ys, kw_format = inputs._meta_coords(*ys, which=sy, **kw_format)
1920-
descending = (
1921-
autox
1922-
and autoreverse
1923-
and x.ndim == 1
1924-
and x.size > 1
1925-
and np.all(np.sign(np.diff(inputs._to_numpy_array(x))) == -1)
1926-
)
1927-
if descending and getattr(self, f'get_autoscale{sx}_on')():
1928-
kw_format[sx + 'reverse'] = True
1929-
1930-
# Apply formatting
1931-
if kw_format:
1932-
self.format(**kw_format)
1920+
if autox and autoreverse and inputs._is_descending(x):
1921+
if getattr(self, f'get_autoscale{sx}_on')():
1922+
kw_format[sx + 'reverse'] = True
19331923

1934-
# Finally strip metadata
1924+
# Finally apply formatting and strip metadata
19351925
# WARNING: Most methods that accept 2D arrays use columns of data, but when
19361926
# pandas DataFrame specifically is passed to hist, boxplot, or violinplot, rows
19371927
# of data assumed! Converting to ndarray necessary.
1928+
if kw_format:
1929+
self.format(**kw_format)
19381930
ys = tuple(map(inputs._to_numpy_array, ys))
19391931
if x is not None: # pie() and hist()
19401932
x = inputs._to_numpy_array(x)
@@ -2004,14 +1996,9 @@ def _parse_2d_format(
20041996
x, kw_format = inputs._meta_coords(x, which='x', **kw_format)
20051997
y, kw_format = inputs._meta_coords(y, which='y', **kw_format)
20061998
for s, d in zip('xy', (x, y)):
2007-
descending = (
2008-
autoreverse
2009-
and d.ndim == 1
2010-
and d.size > 1
2011-
and np.all(np.sign(np.diff(inputs._to_numpy_array(d))) == -1)
2012-
)
2013-
if descending and getattr(self, f'get_autoscale{s}_on')():
2014-
kw_format[s + 'reverse'] = True
1999+
if autoreverse and inputs._is_descending(d):
2000+
if getattr(self, f'get_autoscale{s}_on')():
2001+
kw_format[s + 'reverse'] = True
20152002

20162003
# Apply formatting
20172004
if kw_format:

proplot/colors.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030

3131
from .config import rc
3232
from .internals import ic # noqa: F401
33-
from .internals import _kwargs_to_args, _not_none, _pop_props, docstring, warnings
33+
from .internals import (
34+
_kwargs_to_args,
35+
_not_none,
36+
_pop_props,
37+
docstring,
38+
inputs,
39+
warnings,
40+
)
3441
from .utils import set_alpha, to_hex, to_rgb, to_rgba, to_xyz, to_xyza
3542

3643
__all__ = [
@@ -2305,12 +2312,12 @@ def _sanitize_levels(levels):
23052312
"""
23062313
Ensure the levels are monotonic. If they are descending, reverse them.
23072314
"""
2308-
levels = np.atleast_1d(levels)
2315+
levels = inputs._to_numpy_array(levels)
23092316
if levels.ndim != 1 or levels.size < 2:
23102317
raise ValueError(f'Levels {levels} must be a 1D array with size >= 2.')
23112318
if isinstance(levels, ma.core.MaskedArray):
23122319
levels = levels.filled(np.nan)
2313-
if not np.all(np.isfinite(levels)):
2320+
if not np.all(np.isfinite(levels)) or not inputs._is_numeric(levels):
23142321
raise ValueError(f'Levels {levels} contain invalid values.')
23152322
diffs = np.sign(np.diff(levels))
23162323
if np.all(diffs == 1):

proplot/internals/inputs.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ def _is_categorical(data):
8888
)
8989

9090

91+
def _is_descending(data):
92+
"""
93+
Test whether the input data is descending. This is used for auto axis reversal.
94+
"""
95+
# NOTE: Want this to work with e.g. datetime axes but fail for strange
96+
# mixed types so filter against object dtypes rather than using _is_numeric
97+
data = _to_numpy_array(data)
98+
return (
99+
data.ndim == 1
100+
and data.size > 1
101+
and data.dtype != 'O'
102+
and np.all(np.sign(np.diff(data)) == -1)
103+
)
104+
105+
91106
def _to_duck_array(data, strip_units=False):
92107
"""
93108
Convert arbitrary input to duck array. Preserve array containers with metadata.

0 commit comments

Comments
 (0)