Skip to content

Commit c32a671

Browse files
authored
Merge pull request #25294 from meeseeksmachine/auto-backport-of-pr-25278-on-v3.7.x
Backport PR #25278 on branch v3.7.x (Revert #23417 (Consistently set label on axis with units))
2 parents c084c96 + daccc71 commit c32a671

File tree

6 files changed

+22
-30
lines changed

6 files changed

+22
-30
lines changed

lib/matplotlib/axes/_base.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2545,9 +2545,7 @@ def _process_unit_info(self, datasets=None, kwargs=None, *, convert=True):
25452545
except KeyError:
25462546
raise ValueError(f"Invalid axis name: {axis_name!r}") from None
25472547
# Update from data if axis is already set but no unit is set yet.
2548-
if (axis is not None and
2549-
data is not None and
2550-
not axis._have_units_and_converter()):
2548+
if axis is not None and data is not None and not axis.have_units():
25512549
axis.update_units(data)
25522550
for axis_name, axis in axis_map.items():
25532551
# Return if no axis is set.

lib/matplotlib/axis.py

-9
Original file line numberDiff line numberDiff line change
@@ -1716,17 +1716,8 @@ def _update_axisinfo(self):
17161716
self.set_default_intervals()
17171717

17181718
def have_units(self):
1719-
"""
1720-
Return `True` if units or a converter have been set.
1721-
"""
17221719
return self.converter is not None or self.units is not None
17231720

1724-
def _have_units_and_converter(self):
1725-
"""
1726-
Return `True` if units and a converter have been set.
1727-
"""
1728-
return self.converter is not None and self.units is not None
1729-
17301721
def convert_units(self, x):
17311722
# If x is natively supported by Matplotlib, doesn't need converting
17321723
if munits._is_natively_supported(x):

lib/matplotlib/testing/jpl_units/UnitDbl.py

-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ def __bool__(self):
9191

9292
def _cmp(self, op, rhs):
9393
"""Check that *self* and *rhs* share units; compare them using *op*."""
94-
if rhs is None:
95-
return NotImplemented
9694
self.checkSameUnits(rhs, "compare")
9795
return op(self._value, rhs._value)
9896

lib/matplotlib/tests/test_dates.py

+21
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,27 @@ def test_concise_formatter_show_offset(t_delta, expected):
636636
assert formatter.get_offset() == expected
637637

638638

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

lib/matplotlib/tests/test_units.py

-16
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,3 @@ def test_plot_kernel():
283283
# just a smoketest that fail
284284
kernel = Kernel([1, 2, 3, 4, 5])
285285
plt.plot(kernel)
286-
287-
288-
@pytest.mark.parametrize('plot_meth_name', ['scatter', 'plot'])
289-
def test_unit_axis_label(plot_meth_name):
290-
# Check that the correct Axis labels are set on plots with units
291-
import matplotlib.testing.jpl_units as units
292-
units.register()
293-
294-
fig, ax = plt.subplots()
295-
ax.xaxis.set_units('m')
296-
ax.yaxis.set_units('sec')
297-
plot_method = getattr(ax, plot_meth_name)
298-
plot_method(np.arange(3) * units.m, np.arange(3) * units.sec)
299-
assert ax.get_xlabel() == 'm'
300-
assert ax.get_ylabel() == 'sec'
301-
plt.close('all')

0 commit comments

Comments
 (0)