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).

Notes

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

Notes

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

Notes

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

_do_snap_position(value=None)

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_diff(event)

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)

_get_vertex(event)

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

_get_width_indicator_coords()

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(event)

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

_onmousemove(event)

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

_resize(event)

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

_rotate(event)

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

_set_patch()

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

_set_size(value)

Setter for the ‘size’ property.

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

_update_patch_geometry()

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

_update_patch_position()

Updates the position of the patch on the plot.

_update_patch_size()

Updates the size of the patch on the plot.

_validate_pos(pos)

Make sure all vertices are within axis bounds.

connect_navigate()

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

get_centre()

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

get_line_length()

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

onpick(event)

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’.

Examples

>>> angle_between((1, 0), (0, 1))
1.5707963267948966
>>> angle_between((1, 0), (1, 0))
0.0
>>> angle_between((1, 0), (-1, 0))
3.141592653589793
hyperspy.drawing._widgets.line2d.unit_vector(vector)

Returns the unit vector of the vector.