Skip to content

Conversation

ianthomas23
Copy link
Member

@ianthomas23 ianthomas23 commented Jun 30, 2023

Closes #25114.

Previously polygons generated by tricontourf have finished with a kind code of 2 (MOVETO) rather than 79 (CLOSEPOLY). This wasn't a problem for rendering but was for people doing something with the generated paths such as using to_polygons().

The fix is to ensure that polygons generated by tricontour and tricontourf have their last vertex identical to their first vertex, and the correct CLOSEPOLY code. This is the same as that produced by contour and contourf.

Simple reproducer:

import matplotlib.tri as mtri
import matplotlib.pyplot as plt

x = [0, 1, 1, 0, 0.5]
y = [0, 0, 1, 1, 0.5]
z = [0, 0, 0, 0, 1.0]
triangles = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]]
triang = mtri.Triangulation(x, y, triangles)

_, ax = plt.subplots()
cs = ax.tricontourf(triang, z, levels=[0.5, 2])
print(cs.get_paths()[0])

Before the fix this gives

Path(array([[0.75, 0.25],
            [0.75, 0.75],
            [0.25, 0.75],
            [0.25, 0.25]]), array([1, 2, 2, 2], dtype=uint8))

and after the fix

Path(array([[0.75, 0.25],
            [0.75, 0.75],
            [0.25, 0.75],
            [0.25, 0.25],
            [0.75, 0.25]]), array([ 1,  2,  2,  2, 79], dtype=uint8))

(Edited to avoid using deprecated API)

@ianthomas23 ianthomas23 force-pushed the 25114_tricontourf_closing_code branch from 837235e to bdd6d36 Compare June 30, 2023 18:41
@tacaswell tacaswell added this to the v3.8.0 milestone Jun 30, 2023
@oscargus oscargus merged commit 1e01504 into matplotlib:main Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: matplotlib.path.Path.to_polygons fails with TriContourSet paths
3 participants