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

Skip to content

Commit 590c626

Browse files
committed
Added zorder to clabel, test, whats new entry
1 parent 5d660a4 commit 590c626

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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.

lib/matplotlib/contour.py

Lines changed: 9 additions & 3 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,11 @@ 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 default zorder of `.Text` class is used.
131+
127132
Returns
128133
-------
129134
labels
@@ -144,6 +149,7 @@ def clabel(self, levels=None, *,
144149
# Detect if manual selection is desired and remove from argument list.
145150
self.labelManual = manual
146151
self.rightside_up = rightside_up
152+
self._zorder = zorder
147153

148154
if levels is None:
149155
levels = self.levels
@@ -397,7 +403,7 @@ def _get_label_text(self, x, y, rotation):
397403
dx, dy = self.ax.transData.inverted().transform((x, y))
398404
t = text.Text(dx, dy, rotation=rotation,
399405
horizontalalignment='center',
400-
verticalalignment='center')
406+
verticalalignment='center', zorder=self._zorder)
401407
return t
402408

403409
def _get_label_clabeltext(self, x, y, rotation):
@@ -411,7 +417,7 @@ def _get_label_clabeltext(self, x, y, rotation):
411417
np.array([[x, y]]))
412418
t = ClabelText(dx, dy, rotation=drotation[0],
413419
horizontalalignment='center',
414-
verticalalignment='center')
420+
verticalalignment='center', zorder=self._zorder)
415421

416422
return t
417423

lib/matplotlib/tests/test_contour.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,22 @@ 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):
295+
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
296+
z = np.max(np.dstack([abs(x), abs(y)]), 2)
297+
298+
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)
303+
for clabel in clabels1:
304+
assert clabel.get_zorder() == 1234
305+
for clabel in clabels2:
306+
assert clabel.get_zorder() == 12345
307+
308+
293309
@image_comparison(['contour_log_extension.png'],
294310
remove_text=True, style='mpl20')
295311
def test_contourf_log_extension():

0 commit comments

Comments
 (0)