diff --git a/doc/users/next_whats_new/subfigure_zorder.rst b/doc/users/next_whats_new/subfigure_zorder.rst new file mode 100644 index 000000000000..a740bbda8eb6 --- /dev/null +++ b/doc/users/next_whats_new/subfigure_zorder.rst @@ -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) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index dd76c932b01e..5a231d27fa5f 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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( @@ -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: @@ -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 diff --git a/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure.png b/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure.png index 17dfea29d844..bcfc4494b944 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure.png and b/lib/matplotlib/tests/baseline_images/test_figure/test_subfigure.png differ diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 0e1706f37b59..c3d177425723 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -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...