8000 Merge pull request #16329 from pharshalp/clabel_zorder · matplotlib/matplotlib@34aa790 · GitHub
[go: up one dir, main page]

Skip to content
< 10000 script crossorigin="anonymous" type="application/javascript" src="https://github.githubassets.com/assets/sessions-eed3aa0554dd.js" defer="defer">

Commit 34aa790

Browse files
authored
Merge pull request #16329 from pharshalp/clabel_zorder
ENH: add zorder kwarg to contour clabel (and a better default value for zorder)
2 parents b110097 + 0af446b commit 34aa790

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Set zorder of contour labels
2+
----------------------------
3+
`~.axes.Axes.clabel` now accepts a ``zorder`` kwarg
4+
making it easier to set the ``zorder`` of contour labels.
5+
If not specified, the default ``zorder`` of clabels used to always be 3
6+
(i.e. the default ``zorder`` of `~.text.Text`) irrespective of the ``zorder``
7+
passed to `~.axes.Axes.contour`/`~.axes.Axes.contourf`.
8+
The new default ``zorder`` for clabels has been changed to (2 + ``zorder``
9+
passed to `~.axes.Axes.contour`/`~.axes.Axes.contourf`).

lib/matplotlib/contour.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ContourLabeler:
5151
def clabel(self, levels=None, *,
5252
fontsize=None, inline=True, inline_spacing=5, fmt='%1.3f',
5353
colors=None, use_clabeltext=False, manual=False,
54-
rightside_up=True):
54+
rightside_up=True, zorder=None):
5555
"""
5656
Label a contour plot.
5757
@@ -124,6 +124,12 @@ def clabel(self, levels=None, *,
124124
of texts during the drawing time, therefore this can be used if
125125
aspect of the axes changes.
126126
127+
zorder : float or None, optional
128+
zorder of the contour labels.
129+
130+
If not specified, the zorder of contour labels is set to
131+
(2 + zorder of contours).
132+
127133
Returns
128134
-------
129135
labels
@@ -144,6 +150,10 @@ def clabel(self, levels=None, *,
144150
# Detect if manual selection is desired and remove from argument list.
145151
self.labelManual = manual
146152
self.rightside_up = rightside_up
153+
if zorder is None:
154+
self._clabel_zorder = 2+self._contour_zorder
155+
else:
156+
self._clabel_zorder = zorder
147157

148158
if levels is None:
149159
levels = self.levels
@@ -397,7 +407,7 @@ def _get_label_text(self, x, y, rotation):
397407
dx, dy = self.ax.transData.inverted().transform((x, y))
398408
t = text.Text(dx, dy, rotation=rotation,
399409
horizontalalignment='center',
400-
verticalalignment='center')
410+
verticalalignment='center', zorder=self._clabel_zorder)
401411
return t
402412

403413
def _get_label_clabeltext(self, x, y, rotation):
@@ -411,7 +421,7 @@ def _get_label_clabeltext(self, x, y, rotation):
411421
np.array([[x, y]]))
412422
t = ClabelText(dx, dy, rotation=drotation[0],
413423
horizontalalignment='center',
414-
verticalalignment='center')
424+
verticalalignment='center', zorder=self._clabel_zorder)
415425

416426
return t
417427

@@ -869,7 +879,7 @@ def __init__(self, ax, *args,
869879
self.allkinds = [None] * len(self.allsegs)
870880

871881
# Default zorder taken from Collection
872-
zorder = kwargs.pop('zorder', 1)
882+
self._contour_zorder = kwargs.pop('zorder', 1)
873883
for level, level_upper, segs, kinds in \
874884
zip(lowers, uppers, self.allsegs, self.allkinds):
875885
paths = self._make_paths(segs, kinds)
@@ -880,7 +890,7 @@ def __init__(self, ax, *args,
880890
edgecolors='none',
881891
alpha=self.alpha,
882892
transform=self.get_transform(),
883-
zorder=zorder)
893+
zorder=self._contour_zorder)
884894
self.ax.add_collection(col, autolim=False)
885895
self.collections.append(col)
886896
else:
@@ -891,7 +901,7 @@ def __init__(self, ax, *args,
891901
if aa is not None:
892902
aa = (self.antialiased,)
893903
# Default zorder taken from LineCollection
894-
zorder = kwargs.pop('zorder', 2)
904+
self._contour_zorder = kwargs.pop('zorder', 2)
895905
for level, width, lstyle, segs in \
896906
zip(self.levels, tlinewidths, tlinestyles, self.allsegs):
897907
col = mcoll.LineCollection(
@@ -901,7 +911,7 @@ def __init__(self, ax, *args,
901911
linestyles=[lstyle],
902912
alpha=self.alpha,
903913
transform=self.get_transform(),
904-
zorder=zorder)
914+
zorder=self._contour_zorder)
905915
col.set_label('_nolegend_')
906916
self.ax.add_collection(col, autolim=False)
907917
self.collections.append(col)

lib/matplotlib/tests/test_contour.py

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

292292

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):
297+
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
298+
z = np.max(np.dstack([abs(x), abs(y)]), 2)
299+
300+
fig, (ax1, ax2) = plt.subplots(ncols=2)
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 = 2+contour_zorder
309+
else:
310+
expected_clabel_zorder = clabel_zorder
311+
312+
for clabel in clabels1:
313+
assert clabel.get_zorder() == expected_clabel_zorder
314+
for clabel in clabels2:
315+
assert clabel.get_zorder() == expected_clabel_zorder
316+
317+
293318
@image_comparison(['contour_log_extension.png'],
294319
remove_text=True, style='mpl20')
295320
def test_contourf_log_extension():

0 commit comments

Comments
 (0)
0