Polygon Selector

Polygon Selector#

Shows how to create a polygon programmatically or interactively

import matplotlib.pyplot as plt

from matplotlib.widgets import PolygonSelector

To create the polygon programmatically

fig, ax = plt.subplots()
fig.show()

selector = PolygonSelector(ax, lambda *args: None)

# Add three vertices
selector.verts = [(0.1, 0.4), (0.5, 0.9), (0.3, 0.2)]
../../../_images/1cbaa7a37f0f8e3a611de0bbd33cd686c8bc7c9cfe5aa5108286a06d081d6bf2.png

To create the polygon interactively

fig2, ax2 = plt.subplots()
fig2.show()

selector2 = PolygonSelector(ax2, lambda *args: None)

print("Click on the figure to create a polygon.")
print("Press the 'esc' key to start a new polygon.")
print("Try holding the 'shift' key to move all of the vertices.")
print("Try holding the 'ctrl' key to move a single vertex.")
Click on the figure to create a polygon.
Press the 'esc' key to start a new polygon.
Try holding the 'shift' key to move all of the vertices.
Try holding the 'ctrl' key to move a single vertex.
../../../_images/45816d171484af6f5b3de210bb3ae3d5c2ef9ecbcb6905647d8f0d99ce65e1a7.png

… admonition:: References

The use of the following functions, methods, classes and modules is shown in this example:

  • matplotlib.widgets.PolygonSelector