8000 Merge pull request #26926 from cgadal-forks/subfigure-zorder · matplotlib/matplotlib@cc4a319 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc4a319

Browse files
authored
Merge pull request #26926 from cgadal-forks/subfigure-zorder
Closes #22011: Changes to SubFigures so it behaves like a regular artist
2 parents f976947 + 512da1c commit cc4a319

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Subfigures have now controllable zorders
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
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.
5+
6+
.. plot::
7+
:include-source: true
8+
:alt: Example on controlling the zorder of a subfigure
9+
10+
import matplotlib.pyplot as plt
11+
import numpy as np
12+
x = np.linspace(1, 10, 10)
13+
y1, y2 = x, -x
14+
fig = plt.figure(constrained_layout=True)
15+
subfigs = fig.subfigures(nrows=1, ncols=2)
16+
for subfig in subfigs:
17+
axarr = subfig.subplots(2, 1)
18+
for ax in axarr.flatten():
19+
(l1,) = ax.plot(x, y1, label="line1")
20+
(l2,) = ax.plot(x, y2, label="line2")
21+
subfigs[0].set_zorder(6)
22+
l = fig.legend(handles=[l1, l2], loc="upper center", ncol=2)

lib/matplotlib/figure.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ def __init__(self, **kwargs):
152152
def _get_draw_artists(self, renderer):
153153
"""Also runs apply_aspect"""
154154
artists = self.get_children()
155-
for sfig in self.subfigs:
156-
artists.remove(sfig)
157-
childa = sfig.get_children()
158-
for child in childa:
159-
if child in artists:
160-
artists.remove(child)
161155

162156
artists.remove(self.patch)
163157
artists = sorted(
@@ -2310,8 +2304,6 @@ def draw(self, renderer):
23102304
self.patch.draw(renderer)
23112305
mimage._draw_list_compositing_images(
23122306
renderer, self, artists, self.figure.suppressComposite)
2313-
for sfig in self.subfigs:
2314-
sfig.draw(renderer)
23152307
renderer.close_group('subfigure')
23162308

23172309
finally:
@@ -3117,9 +3109,6 @@ def draw(self, renderer):
31173109
mimage._draw_list_compositing_images(
31183110
renderer, self, artists, self.suppressComposite)
31193111

3120-
for sfig in self.subfigs:
3121-
sfig.draw(renderer)
3122-
31233112
renderer.close_group('figure')
31243113
finally:
31253114
self.stale = False
Loading

lib/matplotlib/tests/test_figure.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,12 @@ def test_subfigure():
12861286

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

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

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

0 commit comments

Comments
 (0)
0