Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bae7d20

Browse files
committed
better default value for clabel zorder
1 parent 590c626 commit bae7d20

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

lib/matplotlib/contour.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ def clabel(self, levels=None, *,
127127
zorder : float or None, optional
128128
zorder of the contour labels.
129129
130-
If not specified, the default zorder of `.Text` class is used.
130+
If not specified, the zorder of contour labels is set to either
131+
(2 + zorder of contours) or (1 + zorder of contours) depending on
132+
whether the contours are filled or not filled.
131133
132134
Returns
133135
-------
@@ -149,7 +151,14 @@ def clabel(self, levels=None, *,
149151
# Detect if manual selection is desired and remove from argument list.
150152
self.labelManual = manual
151153
self.rightside_up = rightside_up
152-
self._zorder = zorder
154+
if zorder is None:
155+
if self.filled:
156+
self._clabel_zorder = 2+self._contour_zorder
157+
else:
158+
self._clabel_zorder = 1+self._contour_zorder
159+
else:
160+
self._clabel_zorder = zorder
161+
153162

154163
if levels is None:
155164
levels = self.levels
@@ -403,7 +412,7 @@ def _get_label_text(self, x, y, rotation):
403412
dx, dy = self.ax.transData.inverted().transform((x, y))
404413
t = text.Text(dx, dy, rotation=rotation,
405414
horizontalalignment='center',
406-
verticalalignment='center', zorder=self._zorder)
415+
verticalalignment='center', zorder=self._clabel_zorder)
407416
return t
408417

409418
def _get_label_clabeltext(self, x, y, rotation):
@@ -417,7 +426,7 @@ def _get_label_clabeltext(self, x, y, rotation):
417426
np.array([[x, y]]))
418427
t = ClabelText(dx, dy, rotation=drotation[0],
419428
horizontalalignment='center',
420-
verticalalignment='center', zorder=self._zorder)
429+
verticalalignment='center', zorder=self._clabel_zorder)
421430

422431
return t
423432

@@ -875,7 +884,7 @@ def __init__(self, ax, *args,
875884
self.allkinds = [None] * len(self.allsegs)
876885

877886
# Default zorder taken from Collection
878-
zorder = kwargs.pop('zorder', 1)
887+
self._contour_zorder = kwargs.pop('zorder', 1)
879888
for level, level_upper, segs, kinds in \
880889
zip(lowers, uppers, self.allsegs, self.allkinds):
881890
paths = self._make_paths(segs, kinds)
@@ -886,7 +895,7 @@ def __init__(self, ax, *args,
886895
edgecolors='none',
887896
alpha=self.alpha,
888897
transform=self.get_transform(),
889-
zorder=zorder)
898+
zorder=self._contour_zorder)
890899
self.ax.add_collection(col, autolim=False)
891900
self.collections.append(col)
892901
else:
@@ -897,7 +906,7 @@ def __init__(self, ax, *args,
897906
if aa is not None:
898907
aa = (self.antialiased,)
899908
# Default zorder taken from LineCollection
900-
zorder = kwargs.pop('zorder', 2)
909+
self._contour_zorder = kwargs.pop('zorder', 2)
901910
for level, width, lstyle, segs in \
902911
zip(self.levels, tlinewidths, tlinestyles, self.allsegs):
903912
col = mcoll.LineCollection(
@@ -907,7 +916,7 @@ def __init__(self, ax, *args,
907916
linestyles=[lstyle],
908917
alpha=self.alpha,
909918
transform=self.get_transform(),
910-
zorder=zorder)
919+
zorder=self._contour_zorder)
911920
col.set_label('_nolegend_')
912921
self.ax.add_collection(col, autolim=False)
913922
self.collections.append(col)

lib/matplotlib/tests/test_contour.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,31 @@ def test_circular_contour_warning():
290290
assert len(record) == 0
291291

292292

293-
@pytest.mark.parametrize("use_clabeltext", [True, False])
294-
def test_clabel_zorder(use_clabeltext):
293+
@pytest.mark.parametrize("use_clabeltext, contour_zorder, clabel_zorder",
294+
[(True, 123, 1234), (False, 123, 1234),
295+
(True, 123, None), (False, 123, None)])
296+
def test_clabel_zorder(use_clabeltext, contour_zorder, clabel_zorder):
295297
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
296298
z = np.max(np.dstack([abs(x), abs(y)]), 2)
297299

298300
fig, (ax1, ax2) = plt.subplots(ncols=2)
299-
cs1 = ax1.contour(x, y, z)
300-
cs2 = ax2.contourf(x, y, z)
301-
clabels1 = cs1.clabel(zorder=1234, use_clabeltext=use_clabeltext)
302-
clabels2 = cs2.clabel(zorder=12345, use_clabeltext=use_clabeltext)
301+
cs = ax1.contour(x, y, z, zorder=contour_zorder)
302+
cs_filled = ax2.contourf(x, y, z, zorder=contour_zorder)
303+
clabels1 = cs.clabel(zorder=clabel_zorder, use_clabeltext=use_clabeltext)
304+
clabels2 = cs_filled.clabel(zorder=clabel_zorder,
305+
use_clabeltext=use_clabeltext)
306+
307+
if clabel_zorder is None:
308+
expected_clabel_zorder = 1+contour_zorder
309+
expected_filled_clabel_zorder = 2+contour_zorder
310+
else:
311+
expected_clabel_zorder = clabel_zorder
312+
expected_filled_clabel_zorder = clabel_zorder
313+
303314
for clabel in clabels1:
304-
assert clabel.get_zorder() == 1234
315+
assert clabel.get_zorder() == expected_clabel_zorder
305316
for clabel in clabels2:
306-
assert clabel.get_zorder() == 12345
317+
assert clabel.get_zorder() == expected_filled_clabel_zorder
307318

308319

309320
@image_comparison(['contour_log_extension.png'],

0 commit comments

Comments
 (0)