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

Skip to content

Commit bdd06dd

Browse files
committed
BUG: raise ValueError if sharex, sharey point to a different figure
Sharex and sharey are only supported for sharing within a single figure. Closes #2790.
1 parent eb6e422 commit bdd06dd

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ def __init__(self, fig, rect,
435435
436436
Optional keyword arguments:
437437
438-
================ =========================================
438+
================ ==============================================
439439
Keyword Description
440-
================ =========================================
440+
================ ==============================================
441441
*adjustable* [ 'box' | 'datalim' | 'box-forced']
442442
*alpha* float: the alpha transparency (can be None)
443443
*anchor* [ 'C', 'SW', 'S', 'SE', 'E', 'NE', 'N',
@@ -458,10 +458,10 @@ def __init__(self, fig, rect,
458458
toolbar button status
459459
*position* [left, bottom, width, height] in
460460
class:`~matplotlib.figure.Figure` coords
461-
*sharex* an class:`~matplotlib.axes.Axes` instance
462-
to share the x-axis with
463-
*sharey* an class:`~matplotlib.axes.Axes` instance
464-
to share the y-axis with
461+
*sharex* another class:`~matplotlib.axes.Axes` instance
462+
in *fig* to share the x-axis with
463+
*sharey* another class:`~matplotlib.axes.Axes` instance
464+
in *fig* to share the y-axis with
465465
*title* the title string
466466
*visible* [ *True* | *False* ] whether the axes is
467467
visible
@@ -475,7 +475,7 @@ def __init__(self, fig, rect,
475475
*yscale* [%(scale)s]
476476
*yticklabels* sequence of strings
477477
*yticks* sequence of floats
478-
================ =========================================
478+
================ ==============================================
479479
""" % {'scale': ' | '.join(
480480
[repr(x) for x in mscale.get_scale_names()])}
481481
martist.Artist.__init__(self)
@@ -494,13 +494,17 @@ def __init__(self, fig, rect,
494494
self._sharex = sharex
495495
self._sharey = sharey
496496
if sharex is not None:
497+
if sharex.get_figure() is not fig:
498+
raise ValueError('Shared Axes must be in the same figure')
497499
self._shared_x_axes.join(self, sharex)
498500
if sharex._adjustable == 'box':
499501
sharex._adjustable = 'datalim'
500502
# warnings.warn(
501503
# 'shared axes: "adjustable" is being changed to "datalim"')
502504
self._adjustable = 'datalim'
503505
if sharey is not None:
506+
if sharey.get_figure() is not fig:
507+
raise ValueError('Shared Axes must be in the same figure')
504508
self._shared_y_axes.join(self, sharey)
505509
if sharey._adjustable == 'box':
506510
sharey._adjustable = 'datalim'

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4220,6 +4220,15 @@ def test_empty_shared_subplots():
42204220
assert y1 >= 6
42214221

42224222

4223+
def test_shared_fig_fails():
4224+
# Github Issue #2790, sharing works only within a figure
4225+
fig, ax = plt.subplots()
4226+
with pytest.raises(ValueError):
4227+
plt.subplots(sharex=ax)
4228+
with pytest.raises(ValueError):
4229+
plt.subplots(sharey=ax)
4230+
4231+
42234232
def test_relim_visible_only():
42244233
x1 = (0., 10.)
42254234
y1 = (0., 10.)

0 commit comments

Comments
 (0)