Skip to content
Open
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
8 changes: 6 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,9 @@ def sharex(self, other):
self.xaxis.major = other.xaxis.major # Ticker instances holding
self.xaxis.minor = other.xaxis.minor # locator and formatter.
x0, x1 = other.get_xlim()
self.set_xlim(x0, x1, emit=False, auto=other.get_autoscalex_on())
self.xaxis._set_lim(
x0, x1, emit=False, auto=other.get_autoscalex_on(),
propagate=False)
self.xaxis._scale = other.xaxis._scale

def sharey(self, other):
Expand All @@ -1323,7 +1325,9 @@ def sharey(self, other):
self.yaxis.major = other.yaxis.major # Ticker instances holding
self.yaxis.minor = other.yaxis.minor # locator and formatter.
y0, y1 = other.get_ylim()
self.set_ylim(y0, y1, emit=False, auto=other.get_autoscaley_on())
self.yaxis._set_lim(
y0, y1, emit=False, auto=other.get_autoscaley_on(),
propagate=False)
self.yaxis._scale = other.yaxis._scale

def __clear(self):
Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ def set_default_intervals(self):
# attribute, and the derived code below will check for that
# and use it if it's available (else just use 0..1)

def _set_lim(self, v0, v1, *, emit=True, auto):
def _set_lim(self, v0, v1, *, emit=True, auto, propagate=True):
"""
Set view limits.

Expand All @@ -1336,6 +1336,8 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
auto : bool or None, default: False
Whether to turn on autoscaling of the x-axis. True turns on, False
turns off, None leaves unchanged.
propagate : bool, default: True
Whether to propagate the limits to shared axes.
"""
name = self._get_axis_name()

Expand Down Expand Up @@ -1381,11 +1383,14 @@ def _set_lim(self, v0, v1, *, emit=True, auto):

if emit:
self.axes.callbacks.process(f"{name}lim_changed", self.axes)

if propagate:
# Call all of the other Axes that are shared with this one
for other in self._get_shared_axes():
if other is self.axes:
continue
other._axis_map[name]._set_lim(v0, v1, emit=False, auto=auto)
other._axis_map[name]._set_lim(
v0, v1, emit=False, auto=auto, propagate=False)
if emit:
other.callbacks.process(f"{name}lim_changed", other)
if ((other_fig := other.get_figure(root=False)) !=
Expand Down
12 changes: 11 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10005,13 +10005,23 @@ def test_set_secondary_axis_color():


def test_xylim_changed_shared():
fig, axs = plt.subplots(2, sharex=True, sharey=True)
fig, axs = plt.subplots(3, sharex=True, sharey=True)
events = []
axs[1].callbacks.connect("xlim_changed", events.append)
axs[1].callbacks.connect("ylim_changed", events.append)
axs[0].set(xlim=[1, 3], ylim=[2, 4])
assert events == [axs[1], axs[1]]

axs[0].callbacks.connect("xlim_changed", events.append)
axs[0].callbacks.connect("ylim_changed", events.append)
events.clear()
axs[0].set_xlim(4, 5, emit=False)
axs[0].set_ylim(6, 7, emit=False)
for ax in axs[1:]:
assert ax.get_xlim() == (4, 5)
assert ax.get_ylim() == (6, 7)
assert events == []


@image_comparison(["axhvlinespan_interpolation.png"], style="default")
def test_axhvlinespan_interpolation():
Expand Down
Loading