Skip to content

Commit f8b6a5c

Browse files
committed
New tests to increase coverage
1 parent 6ff4539 commit f8b6a5c

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed
21.9 KB
Loading
5.98 KB
Loading

lib/matplotlib/tests/test_contour.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,59 @@ def test_contour_closed_line_loop():
410410
ax.contour(z, [0.5], linewidths=[20], alpha=0.7)
411411
ax.set_xlim(-0.1, 2.1)
412412
ax.set_ylim(-0.1, 3.1)
413+
414+
415+
def test_quadcontourset_reuse():
416+
# If QuadContourSet returned from one contour(f) call is passed as first
417+
# argument to another the underlying C++ contour generator will be reused.
418+
x, y = np.meshgrid([0.0, 1.0], [0.0, 1.0])
419+
z = x + y
420+
fig, ax = plt.subplots()
421+
qcs1 = ax.contourf(x, y, z)
422+
qcs2 = ax.contour(x, y, z)
423+
assert qcs2._contour_generator != qcs1._contour_generator
424+
qcs3 = ax.contour(qcs1, z)
425+
assert qcs3._contour_generator == qcs1._contour_generator
426+
427+
428+
@image_comparison(baseline_images=['contour_manual'],
429+
extensions=['png'], remove_text=True)
430+
def test_contour_manual():
431+
# Manually specifying contour lines/polygons to plot.
432+
from matplotlib.contour import ContourSet
433+
434+
fig, ax = plt.subplots(figsize=(4, 4))
435+
cmap = 'viridis'
436+
437+
# Segments only (no 'kind' codes).
438+
lines0 = [[[2, 0], [1, 2], [1, 3]]] # Single line.
439+
lines1 = [[[3, 0], [3, 2]], [[3, 3], [3, 4]]] # Two lines.
440+
filled01 = [[[0, 0], [0, 4], [1, 3], [1, 2], [2, 0]]]
441+
filled12 = [[[2, 0], [3, 0], [3, 2], [1, 3], [1, 2]], # Two polygons.
442+
[[1, 4], [3, 4], [3, 3]]]
443+
ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap=cmap)
444+
ContourSet(ax, [1, 2], [lines0, lines1], linewidths=3, colors=['r', 'k'])
445+
446+
# Segments and kind codes (1 = MOVETO, 2 = LINETO, 79 = CLOSEPOLY).
447+
segs = [[[4, 0], [7, 0], [7, 3], [4, 3], [4, 0],
448+
[5, 1], [5, 2], [6, 2], [6, 1], [5, 1]]]
449+
kinds = [[1, 2, 2, 2, 79, 1, 2, 2, 2, 79]] # Polygon containing hole.
450+
ContourSet(ax, [2, 3], [segs], [kinds], filled=True, cmap=cmap)
451+
ContourSet(ax, [2], [segs], [kinds], colors='k', linewidths=3)
452+
453+
454+
@image_comparison(baseline_images=['contour_line_start_on_corner_edge'],
455+
extensions=['png'], remove_text=True)
456+
def test_contour_line_start_on_corner_edge():
457+
fig, ax = plt.subplots(figsize=(6, 5))
458+
459+
x, y = np.meshgrid([0, 1, 2, 3, 4], [0, 1, 2])
460+
z = 1.2 - (x - 2)**2 + (y - 1)**2
461+
mask = np.zeros_like(z, dtype=bool)
462+
mask[1, 1] = mask[1, 3] = True
463+
z = np.ma.array(z, mask=mask)
464+
465+
filled = ax.contourf(x, y, z, corner_mask=True)
466+
cbar = fig.colorbar(filled)
467+
lines = ax.contour(x, y, z, corner_mask=True, colors='k')
468+
cbar.add_lines(lines)

lib/matplotlib/tests/test_triangulation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,17 @@ def test_tricontour_non_finite_z():
11631163
with pytest.raises(ValueError, match='z must not contain masked points '
11641164
'within the triangulation'):
11651165
plt.tricontourf(triang, np.ma.array([0, 1, 2, 3], mask=[1, 0, 0, 0]))
1166+
1167+
1168+
def test_tricontourset_reuse():
1169+
# If TriContourSet returned from one tricontour(f) call is passed as first
1170+
# argument to another the underlying C++ contour generator will be reused.
1171+
x = [0.0, 0.5, 1.0]
1172+
y = [0.0, 1.0, 0.0]
1173+
z = [1.0, 2.0, 3.0]
1174+
fig, ax = plt.subplots()
1175+
tcs1 = ax.tricontourf(x, y, z)
1176+
tcs2 = ax.tricontour(x, y, z)
1177+
assert tcs2._contour_generator != tcs1._contour_generator
1178+
tcs3 = ax.tricontour(tcs1, z)
1179+
assert tcs3._contour_generator == tcs1._contour_generator

lib/matplotlib/tri/tricontour.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def _process_args(self, *args, **kwargs):
3636
C = args[0]._contour_generator
3737
if self.levels is None:
3838
self.levels = args[0].levels
39+
self.zmin = args[0].zmin
40+
self.zmax = args[0].zmax
41+
self._mins = args[0]._mins
42+
self._maxs = args[0]._maxs
3943
else:
4044
from matplotlib import _tri
4145
tri, z = self._contour_args(args, kwargs)

0 commit comments

Comments
 (0)