Skip to content

[ENH]: ContourSet.set_paths #71

@rowan-stein

Description

@rowan-stein

Problem

To get contour labelling working with its special transforms, Cartopy has a workaround where it replaces all the paths on the ContourSet with transformed versions. This currently looks like

paths = cs.get_paths()
paths[:] = transformed_paths

which doesn’t smell very good.

Proposed solution

The above would smell better as

cs.set_paths(transformed_paths)

Additional context

I'm actually a bit confused as to why Collection.set_paths raises NotImplementedError instead of setting self._paths (which is what Collection.get_paths already returns).

Thanks @anntzer. So would it be enough to just copy what PathCollection has?

https://github.com/matplotlib/matplotlib/blob/2a4d905ff2e6493264190f07113dc7f2115a8c1c/lib/matplotlib/collections.py#L1004-L1006

Untested, but possibly yes?


Specification (from research)

  • Add ContourSet.set_paths(paths) to replace the internal list of matplotlib.path.Path objects used for drawing and labeling.
  • Validation:
    • paths must be a sequence of Path instances.
    • If self._paths exists, len(paths) must match len(self._paths) to preserve style/level mapping.
  • Behavior:
    • Assign self._paths = list(paths).
    • If present, clear deprecated split-collections cache (e.g., del self._old_style_split_collections).
    • Mark self.stale = True.
    • No implicit transform application; caller supplies paths in the coordinate system of cs.get_transform().
  • Placement:
    • Implement in lib/matplotlib/contour.py within class ContourSet.
    • Add typing stub in lib/matplotlib/contour.pyi.
  • Tests:
    • Replacing paths should not crash and should mark .stale.
    • Labeling (clabel) works with transformed paths.
    • Existing behavior unchanged when not calling set_paths.

This Issue tracks task ID: 285.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions