Skip to content

Revert #23417 (Consistently set label on axis with units) #25278

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 7 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2545,9 +2545,7 @@ def _process_unit_info(self, datasets=None, kwargs=None, *, convert=True):
except KeyError:
raise ValueError(f"Invalid axis name: {axis_name!r}") from None
# Update from data if axis is already set but no unit is set yet.
if (axis is not None and
data is not None and
not axis._have_units_and_converter()):
if axis is not None and data is not None and not axis.have_units():
axis.update_units(data)
for axis_name, axis in axis_map.items():
# Return if no axis is set.
Expand Down
9 changes: 0 additions & 9 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,17 +1728,8 @@ def _update_axisinfo(self):
self.set_default_intervals()

def have_units(self):
"""
Return `True` if units or a converter have been set.
"""
return self.converter is not None or self.units is not None

def _have_units_and_converter(self):
"""
Return `True` if units and a converter have been set.
"""
return self.converter is not None and self.units is not None

def convert_units(self, x):
# If x is natively supported by Matplotlib, doesn't need converting
if munits._is_natively_supported(x):
Expand Down
2 changes: 0 additions & 2 deletions lib/matplotlib/testing/jpl_units/UnitDbl.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ def __bool__(self):

def _cmp(self, op, rhs):
"""Check that *self* and *rhs* share units; compare them using *op*."""
if rhs is None:
return NotImplemented
self.checkSameUnits(rhs, "compare")
return op(self._value, rhs._value)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,27 @@ def test_concise_formatter_show_offset(t_delta, expected):
assert formatter.get_offset() == expected


def test_concise_converter_stays():
# This test demonstrates problems introduced by gh-23417 (reverted in gh-25278)
# In particular, downstream libraries like Pandas had their designated converters
# overridden by actions like setting xlim (or plotting additional points using
# stdlib/numpy dates and string date representation, which otherwise work fine with
# their date converters)
# While this is a bit of a toy example that would be unusual to see it demonstrates
# the same ideas (namely having a valid converter already applied that is desired)
# without introducing additional subclasses.
# See also discussion at gh-25219 for how Pandas was affected
x = [datetime.datetime(2000, 1, 1), datetime.datetime(2020, 2, 20)]
y = [0, 1]
fig, ax = plt.subplots()
ax.plot(x, y)
# Bypass Switchable date converter
ax.xaxis.converter = conv = mdates.ConciseDateConverter()
assert ax.xaxis.units is None
ax.set_xlim(*x)
assert ax.xaxis.converter == conv


def test_offset_changes():
fig, ax = plt.subplots()

Expand Down
16 changes: 0 additions & 16 deletions lib/matplotlib/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,3 @@ def test_plot_kernel():
# just a smoketest that fail
kernel = Kernel([1, 2, 3, 4, 5])
plt.plot(kernel)


@pytest.mark.parametrize('plot_meth_name', ['scatter', 'plot'])
def test_unit_axis_label(plot_meth_name):
# Check that the correct Axis labels are set on plots with units
import matplotlib.testing.jpl_units as units
units.register()

fig, ax = plt.subplots()
ax.xaxis.set_units('m')
ax.yaxis.set_units('sec')
plot_method = getattr(ax, plot_meth_name)
plot_method(np.arange(3) * units.m, np.arange(3) * units.sec)
assert ax.get_xlabel() == 'm'
assert ax.get_ylabel() == 'sec'
plt.close('all')