From 2228a5e03d98cff496ceaa7d2bcd06ae71f4f574 Mon Sep 17 00:00:00 2001 From: Mark Wolfman Date: Thu, 1 Aug 2019 11:17:35 -0500 Subject: [PATCH 1/3] Documentation for using ConnectionPatch across Axes with constrained_layout. When using a figure with contrained layout management, adding a ConnectionPatch between different subplot Axes breaks the layout. The artist should be removed from layout considerations before being added to the axes. This commit notes this fix in the documentation. https://github.com/matplotlib/matplotlib/issues/14907 --- lib/matplotlib/patches.py | 16 ++++++++++++++++ .../intermediate/constrainedlayout_guide.py | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 261dd50a730e..f1410b8b02ab 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -4273,6 +4273,22 @@ def __init__(self, xyA, xyB, coordsA, coordsB=None, Alternatively they can be set to any valid `~matplotlib.transforms.Transform`. + + .. note:: + Using :class:`~matplotlib.patches.ConnectionPatch` across + two :class:`~matplotlib.axes.Axes` is not directly + compatible with :doc:`constrained layout + `. Remove + the patch from layout consideration using + :meth:`~matplotlib.artist.Artist.set_in_layout` before + adding it to the :class:`~matplotlib.axes.Axes`: + + .. code-block:: default + + con = ConnectionPatch(...) + con.set_in_layout(False) + ax.add_artist(con) + """ if coordsB is None: coordsB = coordsA diff --git a/tutorials/intermediate/constrainedlayout_guide.py b/tutorials/intermediate/constrainedlayout_guide.py index be897b7bb6f1..930786d58245 100644 --- a/tutorials/intermediate/constrainedlayout_guide.py +++ b/tutorials/intermediate/constrainedlayout_guide.py @@ -581,6 +581,12 @@ def docomplicated(suptitle=None): # # * There are small differences in how the backends handle rendering fonts, # so the results will not be pixel-identical. +# +# * A :class:`~matplotlib.patches.ConnectionPatch` across two +# :class:`~matplotlib.axes.Axes` must be removed from the layout +# using :meth:`~matplotlib.artist.Artist.set_in_layout` before +# calling :meth:`~matplotlib.axes.Axes.add_artist`. See +# :class:`~matplotlib.patches.ConnectionPatch` for more information. ########################################################### # Debugging From 10d9c0f580654684b71a500361bea2735b7feb96 Mon Sep 17 00:00:00 2001 From: Mark Wolfman Date: Sat, 3 Aug 2019 11:33:54 -0500 Subject: [PATCH 2/3] Made documentation more general for artist limitations in constrained_layout. --- tutorials/intermediate/constrainedlayout_guide.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tutorials/intermediate/constrainedlayout_guide.py b/tutorials/intermediate/constrainedlayout_guide.py index 930786d58245..ab909115faa1 100644 --- a/tutorials/intermediate/constrainedlayout_guide.py +++ b/tutorials/intermediate/constrainedlayout_guide.py @@ -582,11 +582,13 @@ def docomplicated(suptitle=None): # * There are small differences in how the backends handle rendering fonts, # so the results will not be pixel-identical. # -# * A :class:`~matplotlib.patches.ConnectionPatch` across two -# :class:`~matplotlib.axes.Axes` must be removed from the layout -# using :meth:`~matplotlib.artist.Artist.set_in_layout` before -# calling :meth:`~matplotlib.axes.Axes.add_artist`. See -# :class:`~matplotlib.patches.ConnectionPatch` for more information. +# * An :meth:`~matplotlib.artist.Artist` using axes coordinates will +# result in unusual layouts when added to an +# :class:`~matplotlib.axes.Axes` if the artist extends beyond the +# axes boundary. This can be avoided by passing *False* to +# :meth:`~matplotlib.artist.Artist.set_in_layout` before calling +# :meth:`~matplotlib.axes.Axes.add_artist`. See +# :class:`~matplotlib.patches.ConnectionPatch` for an example. ########################################################### # Debugging From 03ce63965d0637f2c387e286073fc4c698ef022e Mon Sep 17 00:00:00 2001 From: Mark Wolfman Date: Wed, 14 Aug 2019 13:49:36 -0500 Subject: [PATCH 3/3] Revised the ConnectionPatch documentation to suggest Figure.add_artist(). --- lib/matplotlib/patches.py | 21 +++++++++---------- .../intermediate/constrainedlayout_guide.py | 11 +++++----- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index f1410b8b02ab..6d64961bd37f 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -4229,8 +4229,7 @@ def __init__(self, xyA, xyB, coordsA, coordsB=None, clip_on=False, dpi_cor=1., **kwargs): - """ - Connect point *xyA* in *coordsA* with point *xyB* in *coordsB* + """Connect point *xyA* in *coordsA* with point *xyB* in *coordsB* Valid keys are @@ -4275,19 +4274,19 @@ def __init__(self, xyA, xyB, coordsA, coordsB=None, `~matplotlib.transforms.Transform`. .. note:: + Using :class:`~matplotlib.patches.ConnectionPatch` across - two :class:`~matplotlib.axes.Axes` is not directly - compatible with :doc:`constrained layout - `. Remove - the patch from layout consideration using - :meth:`~matplotlib.artist.Artist.set_in_layout` before - adding it to the :class:`~matplotlib.axes.Axes`: + two :class:`~matplotlib.axes.Axes` instances is not + directly compatible with :doc:`constrained layout + `. Add the + artist directly to the :class:`~matplotlib.figure.Figure` + instead of adding it to a specific Axes. .. code-block:: default - con = ConnectionPatch(...) - con.set_in_layout(False) - ax.add_artist(con) + fig, ax = plt.subplots(1, 2, constrained_layout=True) + con = ConnectionPatch(..., axesA=ax[0], axesB=ax[1]) + fig.add_artist(con) """ if coordsB is None: diff --git a/tutorials/intermediate/constrainedlayout_guide.py b/tutorials/intermediate/constrainedlayout_guide.py index ab909115faa1..57597a02d9f4 100644 --- a/tutorials/intermediate/constrainedlayout_guide.py +++ b/tutorials/intermediate/constrainedlayout_guide.py @@ -582,12 +582,11 @@ def docomplicated(suptitle=None): # * There are small differences in how the backends handle rendering fonts, # so the results will not be pixel-identical. # -# * An :meth:`~matplotlib.artist.Artist` using axes coordinates will -# result in unusual layouts when added to an -# :class:`~matplotlib.axes.Axes` if the artist extends beyond the -# axes boundary. This can be avoided by passing *False* to -# :meth:`~matplotlib.artist.Artist.set_in_layout` before calling -# :meth:`~matplotlib.axes.Axes.add_artist`. See +# * An artist using axes coordinates that extend beyond the axes +# boundary will result in unusual layouts when added to an +# axes. This can be avoided by adding the artist directly to the +# :class:`~matplotlib.figure.Figure` using +# :meth:`~matplotlib.figure.Figure.add_artist`. See # :class:`~matplotlib.patches.ConnectionPatch` for an example. ###########################################################