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

Skip to content

Commit 3f53903

Browse files
committed
Fix bug with tricontour triangles
1 parent aba29cf commit 3f53903

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

lib/matplotlib/contour.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,10 +917,11 @@ def set_data(self, *args, **kwargs):
917917
Only the keywords of that function that control *how the contours are
918918
computed* are accepted, and they keep their current values if not
919919
given: *corner_mask*, *algorithm* and the *xunits*/*yunits* unit
920-
keywords for `~.Axes.contour`. Keywords that control how the contours
921-
*look* (*levels*, *colors*, *cmap*, *linewidths*, ...) raise
922-
`TypeError`; set those with the corresponding `.Collection` setters, or
923-
create a new contour set.
920+
keywords for `~.Axes.contour`, or *triangles* and *mask* for
921+
`~.Axes.tricontour`. Keywords that control how the contours *look*
922+
(*levels*, *colors*, *cmap*, *linewidths*, ...) raise `TypeError`; set
923+
those with the corresponding `.Collection` setters, or create a new
924+
contour set.
924925
925926
Notes
926927
-----

lib/matplotlib/tests/test_triangulation.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,34 @@ def test_tricontourset_reuse():
13231323
assert tcs3._contour_generator == tcs1._contour_generator
13241324

13251325

1326+
@pytest.mark.parametrize("plotter", ["tricontour", "tricontourf"])
1327+
@check_figures_equal()
1328+
def test_tricontour_triangles_kwarg(fig_test, fig_ref, plotter):
1329+
# Passing triangles by keyword used to fall through to Collection.set().
1330+
x = [0.0, 1.0, 2.0, 0.0, 1.0, 0.0]
1331+
y = [0.0, 0.0, 0.0, 1.0, 1.0, 2.0]
1332+
z = [0.0, 1.0, 2.0, 1.0, 2.0, 3.0]
1333+
triangles = [[0, 1, 3], [1, 4, 3], [1, 2, 4], [3, 4, 5]]
1334+
levels = [0.5, 1.5, 2.5]
1335+
getattr(fig_test.subplots(), plotter)(x, y, z, triangles=triangles,
1336+
levels=levels)
1337+
getattr(fig_ref.subplots(), plotter)(x, y, triangles, z, levels=levels)
1338+
1339+
1340+
@check_figures_equal()
1341+
def test_tricontour_set_data(fig_test, fig_ref):
1342+
x = [0.0, 0.5, 1.0, 0.0, 0.5, 0.0]
1343+
y = [0.0, 0.0, 0.0, 0.5, 0.5, 1.0]
1344+
z1 = [0.0, 1.0, 2.0, 1.0, 2.0, 3.0]
1345+
z2 = [3.0, 2.0, 1.0, 2.0, 1.0, 0.0]
1346+
levels = [0.5, 1.5, 2.5]
1347+
triangles = [[0, 1, 3], [1, 4, 3], [1, 2, 4], [3, 4, 5]]
1348+
cs = fig_test.subplots().tricontour(x, y, z1, triangles=triangles,
1349+
levels=levels)
1350+
cs.set_data(x, y, z2, triangles=triangles)
1351+
fig_ref.subplots().tricontour(x, y, z2, triangles=triangles, levels=levels)
1352+
1353+
13261354
@check_figures_equal()
13271355
def test_triplot_with_ls(fig_test, fig_ref):
13281356
x = [0, 2, 1]

lib/matplotlib/tri/_tricontour.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _process_args(self, *args, **kwargs):
4242
self._maxs = args[0]._maxs
4343
else:
4444
from matplotlib import _tri
45-
tri, z = self._contour_args(args, kwargs)
45+
tri, z, kwargs = self._contour_args(args, kwargs)
4646
C = _tri.TriContourGenerator(tri.get_cpp_triangulation(), z)
4747
self._mins = [tri.x.min(), tri.y.min()]
4848
self._maxs = [tri.x.max(), tri.y.max()]
@@ -76,7 +76,9 @@ def _contour_args(self, args, kwargs):
7676
func = 'contourf' if self.filled else 'contour'
7777
raise ValueError(f'Cannot {func} log of negative values.')
7878
self._process_contour_level_args(args, z.dtype)
79-
return (tri, z)
79+
# Return kwargs with the triangulation parameters removed; the caller must
80+
# not see e.g. *triangles* again, or it ends up in Collection.set().
81+
return (tri, z, kwargs)
8082

8183

8284
_docstring.interpd.register(_tricontour_doc="""

0 commit comments

Comments
 (0)