Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d54d3a

Browse files
committed
Synchronize units change in Axis.set_units for shared axis
1 parent f77c0a5 commit 6d54d3a

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4305,37 +4305,3 @@ def get_shared_x_axes(self):
43054305
def get_shared_y_axes(self):
43064306
"""Return a reference to the shared axes Grouper object for y axes."""
43074307
return self._shared_y_axes
4308-
4309-
def set_xunits(self, units, emit=True):
4310-
"""
4311-
Set the x-axis units.
4312-
4313-
Parameters
4314-
----------
4315-
units : units tag
4316-
4317-
emit : bool, default: True
4318-
Whether to notify observers of units change.
4319-
"""
4320-
if emit:
4321-
for ax in self._shared_x_axes.get_siblings(self):
4322-
ax.xaxis.set_units(units)
4323-
else:
4324-
self.xaxis.set_units(units)
4325-
4326-
def set_yunits(self, units, emit=True):
4327-
"""
4328-
Set the y-axis units.
4329-
4330-
Parameters
4331-
----------
4332-
units : units tag
4333-
4334-
emit : bool, default: True
4335-
Whether to notify observers of units change.
4336-
"""
4337-
if emit:
4338-
for ax in self._shared_y_axes.get_siblings(self):
4339-
ax.yaxis.set_units(units)
4340-
else:
4341-
self.yaxis.set_units(units)

lib/matplotlib/axis.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,14 +1530,31 @@ def set_units(self, u):
15301530
Parameters
15311531
----------
15321532
u : units tag
1533+
1534+
Notes
1535+
-----
1536+
The units of any shared axis will also be updated.
15331537
"""
15341538
if u == self.units:
15351539
return
1536-
self.units = u
1537-
self._update_axisinfo()
1538-
self.callbacks.process('units')
1539-
self.callbacks.process('units finalize')
1540-
self.stale = True
1540+
if self is self.axes.xaxis:
1541+
shared = [
1542+
ax.xaxis
1543+
for ax in self.axes.get_shared_x_axes().get_siblings(self.axes)
1544+
]
1545+
elif self is self.axes.yaxis:
1546+
shared = [
1547+
ax.yaxis
1548+
for ax in self.axes.get_shared_y_axes().get_siblings(self.axes)
1549+
]
1550+
else:
1551+
shared = [self]
1552+
for axis in shared:
1553+
axis.units = u
1554+
axis._update_axisinfo()
1555+
axis.callbacks.process('units')
1556+
axis.callbacks.process('units finalize')
1557+
axis.stale = True
15411558

15421559
def get_units(self):
15431560
"""Return the units for axis."""

lib/matplotlib/tests/test_units.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,7 @@ def test_set_xyunits(quantity_converter):
184184
ax2.plot(x, y2)
185185
assert ax1.xaxis.get_units() == ax2.xaxis.get_units() == "hours"
186186
assert ax2.yaxis.get_units() == ax2.yaxis.get_units() == "feet"
187-
ax1.set_xunits("seconds")
188-
ax2.set_yunits("inches")
187+
ax1.xaxis.set_units("seconds")
188+
ax2.yaxis.set_units("inches")
189189
assert ax1.xaxis.get_units() == ax2.xaxis.get_units() == "seconds"
190190
assert ax1.yaxis.get_units() == ax2.yaxis.get_units() == "inches"
191-
fig, (ax1, ax2) = plt.subplots(2, 1, sharex='all', sharey='all')
192-
ax1.plot(x, y1)
193-
ax2.plot(x, y2)
194-
ax1.set_xunits("seconds", emit=False)
195-
ax2.set_yunits("inches", emit=False)
196-
assert ax1.xaxis.get_units() == "seconds"
197-
assert ax2.xaxis.get_units() == "hours"
198-
assert ax1.yaxis.get_units() == "feet"
199-
assert ax2.yaxis.get_units() == "inches"

0 commit comments

Comments
 (0)