hyperspy.drawing._widgets.line2d module

class hyperspy.drawing._widgets.line2d.Line2DWidget(axes_manager, **kwargs)

Bases: hyperspy.drawing.widget.ResizableDraggableWidgetBase

A free-form line on a 2D plot. Enables dragging and moving the end points, but also allows rotation of the widget by moving the mouse beyond the end points of the line.

The widget adds the ‘linewidth’ attribute, which is different from the size in the following regards: ‘linewidth’ is simply the width of the patch drawn from point to point. If ‘size’ is greater than 1, it will in principle select a rotated rectangle. If ‘size’ is greater than 4, the bounds of this rectangle will be visualized by two dashed lines along the outline of this rectangle, instead of a single line in the center.

The widget also adds the attributes ‘radius_resize’, ‘radius_move’ and ‘radius_rotate’ (defaults: 5, 5, 10), which determines the picker radius for resizing, aka. moving the edge points (by picking within ‘radius_resize’ from an edge point); for moving (by picking within ‘radius_move’ from the body of the line); and for rotation (by picking within ‘radius_rotate’ of the edge points on the “outside” of the line). The priority is in the order resize, rotate, move; so the ‘radius_rotate’ should always be larger than ‘radius_resize’ if the function is to be accessible (putting it lower is an easy way to disable the functionality).


This widget’s internal position does not lock to axes points by default.


The ‘position’ is now a 2D tuple: tuple(tuple(x1, x2), tuple(y1, y2))


The ‘size’ property corresponds to line width, so it has a len() of only one.


Snaps position to axes grid. Returns snapped value. If value is passed as an argument, the internal state is left untouched, if not the position attribute is updated to the snapped value.


Get difference in position in event and what is stored in _prev_pos, in value space.

_get_func_from_pos(cx, cy)

Get interaction function from pixel position (cx,cy)


Check bitfield on self.func, and return vertex index.


Get coordinates of width indicators.

The returned format is:

[[[x0A, y0A], [x1A, y1A]], [[x0B, y0B], [x1B, y1B]]] Where A and B refer to the two lines


Move line by drag start position + difference in mouse post from when dragging started (picked).


Delegate to _move(), _resize() or _rotate().


Move vertex by difference from pick / last mouse move. Update ‘_prev_pos’.


Rotate original points by the angle between mouse position and rotation start position (rotation center = line center).


Creates the line, and also creates the width indicators if appropriate.


Setter for the ‘size’ property.

Calls _size_changed to handle size change, if the value has changed.


Set line position, and set width indicator’s if appropriate


Updates the position of the patch on the plot.


Updates the size of the patch on the plot.


Make sure all vertices are within axis bounds.


Connect to the axes_manager such that changes in the widget or in the axes_manager are reflected in the other.


Get the line center, which is simply the mean position of its vertices.


Returns line length in axes coordinates. Requires units on all axes to be the same to make any physical sense.


Pick, and if picked, figure out which function to apply. Also store pouse position for use by _onmousemove. As rotation does not work very well with incremental rotations, the original points are stored if we’re rotating.

hyperspy.drawing._widgets.line2d.angle_between(v1, v2)

Returns the angle in radians between the vectors ‘v1’ and ‘v2’.


>>> angle_between((1, 0), (0, 1))
>>> angle_between((1, 0), (1, 0))
>>> angle_between((1, 0), (-1, 0))

Returns the unit vector of the vector.