Skip to content

Emit xlim_changed on shared axes. #26011

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 1 commit into from
Jul 28, 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
12 changes: 7 additions & 5 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,11 +1241,13 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
self.axes.callbacks.process(f"{name}lim_changed", self.axes)
# Call all of the other axes that are shared with this one
for other in self._get_shared_axes():
if other is not self.axes:
other._axis_map[name]._set_lim(
v0, v1, emit=False, auto=auto)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this just be solved with the one line change of:

# Emit callbacks on shared axes without recursing
other.callbacks.process(f"{name}lim_changed", other)

inside of this for loop/if condition

seems overdesigned to introduce undocumented sentinel behavior to get one line to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that's much better.

if other.figure != self.figure:
other.figure.canvas.draw_idle()
if other is self.axes:
continue
other._axis_map[name]._set_lim(v0, v1, emit=False, auto=auto)
if emit:
other.callbacks.process(f"{name}lim_changed", other)
Comment on lines +1247 to +1248
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if emit:
other.callbacks.process(f"{name}lim_changed", other)
other.callbacks.process(f"{name}lim_changed", other)

This if will never be false as it is guarded by the same if emit: on line 1228 (with no overwriting of the variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but I think guarding the shared-axes syncing with if emit: is actually wrong (#26085), whereas the extra if emit: here is correct, so perhaps let's keep it there so that we don't forget to put it in when removing the outer check?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering a bit about that behavior, but didn't take the time to actually test it...

Could it be as simple as dedenting the for loop (and keeping this if)?

That would:

  • call the code to actually update the shared axes, regardless of emit
  • make emit truly only tied to the callback behavior, which is what I would expect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dedenting the for loop will lead to infinite recursion, so that will need some kind of marker to block recursion (but it can't be done by overloading the semantics of emit as I proposed initially...).

if other.figure != self.figure:
other.figure.canvas.draw_idle()

self.stale = True
return v0, v1
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8794,3 +8794,12 @@ def test_set_secondary_axis_color():
assert mcolors.same_color(sax.xaxis.get_tick_params()["color"], "red")
assert mcolors.same_color(sax.xaxis.get_tick_params()["labelcolor"], "red")
assert mcolors.same_color(sax.xaxis.label.get_color(), "red")


def test_xylim_changed_shared():
fig, axs = plt.subplots(2, 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]]