Skip to content

ENH: Support open polygonal chains in PolygonSelector#31623

Draft
lucaznch wants to merge 1 commit intomatplotlib:mainfrom
lucaznch:open-polygonal-chain
Draft

ENH: Support open polygonal chains in PolygonSelector#31623
lucaznch wants to merge 1 commit intomatplotlib:mainfrom
lucaznch:open-polygonal-chain

Conversation

@lucaznch
Copy link
Copy Markdown

@lucaznch lucaznch commented May 6, 2026

PR summary

PolygonSelector is a widget class used to select a polygonal region within an Axes. More precisely, it enables the selection of a closed polygonal chain, i.e., a sequence of vertices connected by line segments where the first and last vertices are connected. These chains may be non-self-intersecting or self-intersecting. Before or after a polygon is defined, the class provides interactive editing capabilities such as repositioning and removing vertices.

An open polygonal chain is a sequence of vertices connected by line segments but where the first and last vertices are not connected. The proposed feature is to enable both programmatic and interactive selection of open polygonal chains within a widget class, including both non-self-intersecting and self-intersecting chains.

The approach is to extend the current PolygonSelector class by adding a new parameter,

PolygonSelector(..., closed=True)

to explicitly control whether the polygonal chain is open or closed. By default the parameter selects closed polygonal regions, preserving the current behavior.

The selection of an open polygonal chain is completed by pressing the Enter key after inserting a vertex. The existing interactive editing functionality is preserved and applies consistently to both modes.

Why is this change necessary / what problem does it solve?

PolygonSelector currently supports only closed polygonal regions, while LassoSelector provides freeform selection without vertex control. There is no widget that supports structured, vertex-based selection of open polygonal chains, which are useful for tasks such as defining paths, annotations, or piecewise geometries where closure is not desired.

What is the reasoning for this implementation?

Several implementation approaches were considered in addition to the current one:

  1. Introduce an OpenPolygonalChainSelector class dedicated to open chains.

  2. Introduce a PolygonalChainSelector class supporting both open and closed chains.

  3. Introduce an internal _PolygonalChainSelector base class to provide common functionality for the existing PolygonSelector and a new OpenPolygonalChainSelector.

Other design approaches were also considered:

  1. For a class supporting both chain types, infer whether the chain is open or closed based on user interaction, instead of relying on a new explicit parameter.

  2. Use the Strategy behavioral design pattern in the current implementation to remove the conditional branches (if self.closed ... else ...) added to handle open and closed chain behavior.

The chosen approach is the simplest one. It introduces a minimal API change and serves as an effective starting point. It preserves the current default behavior and has explicit, predictable behavior, which facilitates testing and documentation. The main drawback is a slight semantic mismatch, as PolygonSelector now supports both closed and open chains.

Example

output1

Co-authored-by: @maf2310

Closes #28421

AI Disclosure

AI was used to help proofread the grammar of this PR description and to understand some parts of the existing codebase during development. The code changes and design decisions were developed independently.

PR checklist

Extend PolygonSelector with open polygonal chain support
by adding a new parameter to explicitly control
whether the chain is open or closed.

The selection of an open polygonal chain is completed
by pressing the Enter key after inserting a vertex.

The existing interactive editing functionality
also applies to open polygonal chains.

Extend existing tests to cover the new mode.

Closes matplotlib#28421

Co-authored-by: Mafalda Botelho <mafaldabotelho@tecnico.ulisboa.pt>
@lucaznch
Copy link
Copy Markdown
Author

lucaznch commented May 6, 2026

Opening this PR as a draft since it's still in progress. The core implementation is in place but it still needs more tests and documentation, and possibly some small code improvements. And it is also meant to allow early feedback on the chosen approach and related aspects.

@timhoffm
Copy link
Copy Markdown
Member

timhoffm commented May 7, 2026

I didn't look at the code yet, but the extension conceptually makes sense, and the implementation via a closed parameter seems reasonable. 👍

@maf2310
Copy link
Copy Markdown
Contributor

maf2310 commented May 7, 2026

Thanks for the feedback!

When starting the implementation we were actually leaning towards the third approach mentioned

Introduce an internal _PolygonalChainSelector base class to provide common functionality for the existing PolygonSelector and a new OpenPolygonalChainSelector.

Although it would make the PR a bit more complex, we believe it would be worthwhile since it would centralize the shared logic and provide a cleaner architecture with clearer semantics for both cases. We also believe the level of difficulty would still be appropriate for us.

We ended up going with the current approach because it seemed like the simplest and least invasive starting point, but we are very interested in hearing thoughts about this alternative approach as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: OpenPolygonSelector

3 participants