Skip to content

Commit 96fe2ec

Browse files
author
Cyril Gadal
committed
Modify test_figure.py::test_subfigure to check for draw zorder
1 parent 4a12124 commit 96fe2ec

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed
682 Bytes
Loading

lib/matplotlib/tests/test_figure.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ def test_figure_clear(clear_meth):
813813
assert fig.axes == []
814814

815815
# c) a figure multiple unnested axes
816-
axes = [fig.add_subplot(2, 1, i+1) for i in range(2)]
816+
axes = [fig.add_subplot(2, 1, i + 1) for i in range(2)]
817817
getattr(fig, clear_meth)()
818818
assert fig.axes == []
819819

@@ -1287,15 +1287,21 @@ def test_subfigure():
12871287

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

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

12911297
def test_subfigure_tightbbox():
12921298
# test that we can get the tightbbox with a subfigure...
12931299
fig = plt.figure(layout='constrained')
12941300
sub = fig.subfigures(1, 2)
12951301

12961302
np.testing.assert_allclose(
1297-
fig.get_tightbbox(fig.canvas.get_renderer()).width,
1298-
8.0)
1303+
fig.get_tightbbox(fig.canvas.get_renderer()).width,
1304+
8.0)
12991305

13001306

13011307
def test_subfigure_dpi():
@@ -1386,11 +1392,11 @@ def test_subfigure_spanning():
13861392

13871393
w = 640
13881394
h = 480
1389-
np.testing.assert_allclose(sub_figs[0].bbox.min, [0., h * 2/3])
1395+
np.testing.assert_allclose(sub_figs[0].bbox.min, [0., h * 2 / 3])
13901396
np.testing.assert_allclose(sub_figs[0].bbox.max, [w / 3, h])
13911397

13921398
np.testing.assert_allclose(sub_figs[1].bbox.min, [w / 3, h / 3])
1393-
np.testing.assert_allclose(sub_figs[1].bbox.max, [w * 2/3, h])
1399+
np.testing.assert_allclose(sub_figs[1].bbox.max, [w * 2 / 3, h])
13941400

13951401
np.testing.assert_allclose(sub_figs[2].bbox.min, [w / 3, 0])
13961402
np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3])
@@ -1431,7 +1437,7 @@ def test_subfigure_ticks():
14311437

14321438

14331439
@image_comparison(['test_subfigure_scatter_size.png'], style='mpl20',
1434-
remove_text=True)
1440+
remove_text=True)
14351441
def test_subfigure_scatter_size():
14361442
# markers in the left- and right-most subplots should be the same
14371443
fig = plt.figure()
@@ -1458,7 +1464,7 @@ def test_subfigure_pdf():
14581464

14591465

14601466
def test_subfigures_wspace_hspace():
1461-
sub_figs = plt.figure().subfigures(2, 3, hspace=0.5, wspace=1/6.)
1467+
sub_figs = plt.figure().subfigures(2, 3, hspace=0.5, wspace=1 / 6.)
14621468

14631469
w = 640
14641470
h = 480
@@ -1482,6 +1488,26 @@ def test_subfigures_wspace_hspace():
14821488
np.testing.assert_allclose(sub_figs[1, 2].bbox.max, [w, h * 0.4])
14831489

14841490

1491+
@check_figures_equal(extensions=["svg", "pdf", "eps", "png"])
1492+
def test_subfigure_drawzorder(fig_test, fig_ref):
1493+
# this tests check that subfigures are actually drawn at the proper zorder
1494+
line = plt.Line2D([0], [0], label='Line')
1495+
handles = [line for i in range(9)]
1496+
fig_ref.set_layout_engine('constrained')
1497+
fig_test.set_layout_engine('constrained')
1498+
1499+
axarr = fig_ref.subplots(2, 2)
1500+
fig_ref.legend(handles=handles, loc='center', ncol=3)
1501+
for ax in axarr[-1]:
1502+
ax.set_zorder(10)
1503+
1504+
subfigs = fig_test.subfigures(nrows=2, ncols=1)
1505+
for subfig in subfigs:
1506+
axarr = subfig.subplots(1, 2)
1507+
subfigs[-1].set_zorder(10)
1508+
fig_test.legend(handles=handles, loc='center', ncol=3)
1509+
1510+
14851511
def test_add_subplot_kwargs():
14861512
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
14871513
fig = plt.figure()
@@ -1614,7 +1640,7 @@ def test_deepcopy():
16141640
def test_unpickle_with_device_pixel_ratio():
16151641
fig = Figure(dpi=42)
16161642
fig.canvas._set_device_pixel_ratio(7)
1617-
assert fig.dpi == 42*7
1643+
assert fig.dpi == 42 * 7
16181644
fig2 = pickle.loads(pickle.dumps(fig))
16191645
assert fig2.dpi == 42
16201646

0 commit comments

Comments
 (0)