Skip to content
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
22 changes: 22 additions & 0 deletions doc/users/next_whats_new/subfigure_zorder.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Subfigures have now controllable zorders
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Previously, setting the zorder of a subfigure had no effect, and those were plotted on top of any figure-level artists (i.e for example on top of fig-level legends). Now, subfigures behave like any other artists, and their zorder can be controlled, with default a zorder of 0.

.. plot::
:include-source: true
:alt: Example on controlling the zorder of a subfigure

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 10, 10)
y1, y2 = x, -x
fig = plt.figure(constrained_layout=True)
subfigs = fig.subfigures(nrows=1, ncols=2)
for subfig in subfigs:
axarr = subfig.subplots(2, 1)
for ax in axarr.flatten():
(l1,) = ax.plot(x, y1, label="line1")
(l2,) = ax.plot(x, y2, label="line2")
subfigs[0].set_zorder(6)
l = fig.legend(handles=[l1, l2], loc="upper center", ncol=2)
11 changes: 0 additions & 11 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ def __init__(self, **kwargs):
def _get_draw_artists(self, renderer):
"""Also runs apply_aspect"""
artists = self.get_children()
for sfig in self.subfigs:
artists.remove(sfig)
childa = sfig.get_children()
for child in childa:
if child in artists:
artists.remove(child)

artists.remove(self.patch)
artists = sorted(
Expand Down Expand Up @@ -2310,8 +2304,6 @@ def draw(self, renderer):
self.patch.draw(renderer)
mimage._draw_list_compositing_images(
renderer, self, artists, self.figure.suppressComposite)
for sfig in self.subfigs:
sfig.draw(renderer)
renderer.close_group('subfigure')

finally:
Expand Down Expand Up @@ -3117,9 +3109,6 @@ def draw(self, renderer):
mimage._draw_list_compositing_images(
renderer, self, artists, self.suppressComposite)

for sfig in self.subfigs:
sfig.draw(renderer)

renderer.close_group('figure')
finally:
self.stale = False
Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_figure/test_subfigure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,12 @@ def test_subfigure():

fig.suptitle('Figure suptitle', fontsize='xx-large')

# below tests for the draw zorder of subfigures.
leg = fig.legend(handles=[plt.Line2D([0], [0], label='Line{}'.format(i))
for i in range(5)], loc='center')
sub[0].set_zorder(leg.get_zorder() - 1)
sub[1].set_zorder(leg.get_zorder() + 1)


def test_subfigure_tightbbox():
# test that we can get the tightbbox with a subfigure...
Expand Down