Skip to content

Consistently set label on axis with units #23417

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 6 commits into from
Nov 26, 2022
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: 3 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,9 @@ 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():
if (axis is not None and
data is not None and
not axis._have_units_and_converter()):
axis.update_units(data)
for axis_name, axis in axis_map.items():
# Return if no axis is set.
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,8 +1652,17 @@ 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: 2 additions & 0 deletions lib/matplotlib/testing/jpl_units/UnitDbl.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ 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.
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,19 @@ 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')