matplotlib.widgets.RectangleSelector#

class matplotlib.widgets.RectangleSelector(ax, onselect=None, *, minspanx=0, minspany=0, useblit=False, props=None, spancoords='data', button=None, grab_range=10, handle_props=None, interactive=False, state_modifier_keys=None, drag_from_anywhere=False, ignore_event_outside=False, use_data_coordinates=False)[source]#

Bases: _SelectorWidget

Select a rectangular region of an Axes.

For the cursor to remain responsive you must keep a reference to it.

Press and release events triggered at the same coordinates outside the selection will clear the selector, except when ignore_event_outside=True.

Parameters:
axAxes

The parent Axes for the widget.

onselectfunction, optional

A callback function that is called after a release event and the selection is created, changed or removed. It must have the signature:

def onselect(eclick: MouseEvent, erelease: MouseEvent)

where eclick and erelease are the mouse click and release MouseEvents that start and complete the selection.

minspanxfloat, default: 0

Selections with an x-span less than or equal to minspanx are removed (when already existing) or cancelled.

minspanyfloat, default: 0

Selections with a y-span less than or equal to minspanx are removed (when already existing) or cancelled.

useblitbool, default: False

Whether to use blitting for faster drawing (if supported by the backend). See the tutorial Faster rendering by using blitting for details.

propsdict, optional

Properties with which the rectangle is drawn. See Patch for valid properties. Default:

dict(facecolor='red', edgecolor='black', alpha=0.2, fill=True)

spancoords{"data", "pixels"}, default: "data"

Whether to interpret minspanx and minspany in data or in pixel coordinates.

buttonMouseButton, list of MouseButton, default: all buttons

Button(s) that trigger rectangle selection.

grab_rangefloat, default: 10

Distance in pixels within which the interactive tool handles can be activated.

handle_propsdict, optional

Properties with which the interactive handles (marker artists) are drawn. See the marker arguments in Line2D for valid properties. Default values are defined in mpl.rcParams except for the default value of markeredgecolor which will be the same as the edgecolor property in props.

interactivebool, default: False

Whether to draw a set of handles that allow interaction with the widget after it is drawn.

state_modifier_keysdict, optional

Keyboard modifiers which affect the widget's behavior. Values amend the defaults, which are:

  • "move": Move the existing shape, default: no modifier.

  • "clear": Clear the current shape, default: "escape".

  • "square": Make the shape square, default: "shift".

  • "center": change the shape around its center, default: "ctrl".

  • "rotate": Rotate the shape around its center between -45° and 45°, default: "r".

"square" and "center" can be combined. The square shape can be defined in data or display coordinates as determined by the use_data_coordinates argument specified when creating the selector.

drag_from_anywherebool, default: False

If True, the widget can be moved by clicking anywhere within its bounds.

ignore_event_outsidebool, default: False

If True, the event triggered outside the span selector will be ignored.

use_data_coordinatesbool, default: False

If True, the "square" shape of the selector is defined in data coordinates instead of display coordinates.

Examples

>>> import matplotlib.pyplot as plt
>>> import matplotlib.widgets as mwidgets
>>> fig, ax = plt.subplots()
>>> ax.plot([1, 2, 3], [10, 50, 100])
>>> def onselect(eclick, erelease):
...     print(eclick.xdata, eclick.ydata)
...     print(erelease.xdata, erelease.ydata)
>>> props = dict(facecolor='blue', alpha=0.5)
>>> rect = mwidgets.RectangleSelector(ax, onselect, interactive=True,
...                                   props=props)
>>> fig.show()
>>> rect.add_state('square')

See also: Rectangle and ellipse selectors

property center#

Center of rectangle in data coordinates.

property corners#

Corners of rectangle in data coordinates from lower left, moving clockwise.

property edge_centers#

Midpoint of rectangle edges in data coordinates from left, moving anti-clockwise.

property extents#

Return (xmin, xmax, ymin, ymax) in data coordinates as defined by the bounding box before rotation.

property geometry#

Return an array of shape (2, 5) containing the x (RectangleSelector.geometry[1, :]) and y (RectangleSelector.geometry[0, :]) data coordinates of the four corners of the rectangle starting and ending in the top left corner.

property rotation#

Rotation in degree in interval [-45°, 45°]. The rotation is limited in range to keep the implementation simple.

Examples using matplotlib.widgets.RectangleSelector#

Rectangle and ellipse selectors

Rectangle and ellipse selectors