Skip to content

BUG: raise ValueError if sharex, sharey point to a different figure #6463

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

Closed
wants to merge 2 commits into from
Closed
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: 8 additions & 0 deletions doc/api/api_changes/2017-06-06-no_share_across_figures.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Axis-sharing between figures is blocked
```````````````````````````````````````

An attempt to share an axis between ``Axes`` objects in different
figures will now raise a ValueError. Previously such sharing
was allowed but could lead to an endless loop when combined
with fixed aspect ratios, as in the case of ``imshow``, for
example.
18 changes: 11 additions & 7 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ def __init__(self, fig, rect,

Optional keyword arguments:

================ =========================================
================ ==============================================
Keyword Description
================ =========================================
================ ==============================================
*adjustable* [ 'box' | 'datalim' | 'box-forced']
*alpha* float: the alpha transparency (can be None)
*anchor* [ 'C', 'SW', 'S', 'SE', 'E', 'NE', 'N',
Expand All @@ -458,10 +458,10 @@ def __init__(self, fig, rect,
toolbar button status
*position* [left, bottom, width, height] in
class:`~matplotlib.figure.Figure` coords
*sharex* an class:`~matplotlib.axes.Axes` instance
to share the x-axis with
*sharey* an class:`~matplotlib.axes.Axes` instance
to share the y-axis with
*sharex* another class:`~matplotlib.axes.Axes` instance
in *fig* to share the x-axis with
*sharey* another class:`~matplotlib.axes.Axes` instance
in *fig* to share the y-axis with
*title* the title string
*visible* [ *True* | *False* ] whether the axes is
visible
Expand All @@ -475,7 +475,7 @@ def __init__(self, fig, rect,
*yscale* [%(scale)s]
*yticklabels* sequence of strings
*yticks* sequence of floats
================ =========================================
================ ==============================================
""" % {'scale': ' | '.join(
[repr(x) for x in mscale.get_scale_names()])}
martist.Artist.__init__(self)
Expand All @@ -494,13 +494,17 @@ def __init__(self, fig, rect,
self._sharex = sharex
self._sharey = sharey
if sharex is not None:
if sharex.get_figure() is not fig:
raise ValueError('Shared Axes must be in the same figure')
self._shared_x_axes.join(self, sharex)
if sharex._adjustable == 'box':
sharex._adjustable = 'datalim'
# warnings.warn(
# 'shared axes: "adjustable" is being changed to "datalim"')
self._adjustable = 'datalim'
if sharey is not None:
if sharey.get_figure() is not fig:
raise ValueError('Shared Axes must be in the same figure')
self._shared_y_axes.join(self, sharey)
if sharey._adjustable == 'box':
sharey._adjustable = 'datalim'
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 @@ -4220,6 +4220,15 @@ def test_empty_shared_subplots():
assert y1 >= 6


def test_shared_fig_fails():
# Github Issue #2790, sharing works only within a figure
fig, ax = plt.subplots()
with pytest.raises(ValueError):
plt.subplots(sharex=ax)
with pytest.raises(ValueError):
plt.subplots(sharey=ax)


def test_relim_visible_only():
x1 = (0., 10.)
y1 = (0., 10.)
Expand Down