Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 840a671

Browse files
committed
Revert to old draw types and add docs about key modifiers
1 parent b60a84d commit 840a671

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

examples/widgets/rectangle_selector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def toggle_selector(event):
4343

4444
# drawtype is 'box' or 'line' or 'none'
4545
toggle_selector.RS = RectangleSelector(current_ax, line_select_callback,
46-
drawtype='box', useblit=True,
46+
drawtype='line', useblit=True,
4747
button=[1, 3], # don't use middle button
4848
minspanx=5, minspany=5,
4949
spancoords='pixels',

lib/matplotlib/widgets.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ def toggle_selector(event):
16601660

16611661
_shape_klass = Rectangle
16621662

1663-
def __init__(self, ax, onselect, drawtype='patch',
1663+
def __init__(self, ax, onselect, drawtype='box',
16641664
minspanx=None, minspany=None, useblit=False,
16651665
lineprops=None, rectprops=None, spancoords='data',
16661666
button=None, maxdist=10, marker_props=None,
@@ -1691,7 +1691,7 @@ def __init__(self, ax, onselect, drawtype='patch',
16911691
Use *drawtype* if you want the mouse to draw a line,
16921692
a box or nothing between click and actual position by setting
16931693
1694-
``drawtype = 'line'``, ``drawtype='patch'`` or ``drawtype = 'none'``.
1694+
``drawtype = 'line'``, ``drawtype='box'`` or ``drawtype = 'none'``.
16951695
16961696
*spancoords* is one of 'data' or 'pixels'. If 'data', *minspanx*
16971697
and *minspanx* will be interpreted in the same coordinates as
@@ -1708,7 +1708,15 @@ def __init__(self, ax, onselect, drawtype='patch',
17081708
3 = right mouse button
17091709
17101710
*interactive* will draw a set of handles and allow you interact
1711-
with the widget after it is drawn.
1711+
with the widget after it is drawn. Holding the 'space' while dragging
1712+
the mouse will move the object.
1713+
1714+
Modifier keys
1715+
-------------
1716+
One can use modifier keys to affect the way the shape is drawn.
1717+
Hold the 'Ctrl' key to center the rectangle on the initial position.
1718+
Hold the 'Shift' key to force the shape to be square.
1719+
These can be combined.
17121720
"""
17131721
_SelectorWidget.__init__(self, ax, onselect, useblit=useblit,
17141722
button=button)
@@ -1717,14 +1725,11 @@ def __init__(self, ax, onselect, drawtype='patch',
17171725
self.visible = True
17181726
self.interactive = interactive
17191727

1720-
if drawtype == 'box': # backwards compatibility
1721-
drawtype = 'patch'
1722-
17231728
if drawtype == 'none':
17241729
drawtype = 'line' # draw a line but make it
17251730
self.visible = False # invisible
17261731

1727-
if drawtype == 'patch':
1732+
if drawtype == 'box':
17281733
if rectprops is None:
17291734
rectprops = dict(facecolor='red', edgecolor='black',
17301735
alpha=0.2, fill=True)
@@ -1739,7 +1744,7 @@ def __init__(self, ax, onselect, drawtype='patch',
17391744
linewidth=2, alpha=0.5)
17401745
lineprops['animated'] = useblit
17411746
self.lineprops = lineprops
1742-
self.to_draw = Line2D([0, 0, 0, 0, 0], [0, 0, 0, 0, 0], visible=False,
1747+
self.to_draw = Line2D([0, 0], [0, 0], visible=False,
17431748
**self.lineprops)
17441749
self.ax.add_line(self.to_draw)
17451750

@@ -1905,7 +1910,7 @@ def _onmove(self, event):
19051910

19061911
@property
19071912
def _rect_bbox(self):
1908-
if self.drawtype == 'patch':
1913+
if self.drawtype == 'box':
19091914
x0 = self.to_draw.get_x()
19101915
y0 = self.to_draw.get_y()
19111916
width = self.to_draw.get_width()
@@ -1972,15 +1977,14 @@ def draw_shape(self, extents):
19721977
xmax = min(xmax, xlim[1])
19731978
ymax = min(ymax, ylim[1])
19741979

1975-
if self.drawtype == 'patch':
1980+
if self.drawtype == 'box':
19761981
self.to_draw.set_x(xmin)
19771982
self.to_draw.set_y(ymin)
19781983
self.to_draw.set_width(xmax - xmin)
19791984
self.to_draw.set_height(ymax - ymin)
19801985

19811986
elif self.drawtype == 'line':
1982-
self.to_draw.set_data([xmin, xmin, xmax, xmax, xmin],
1983-
[ymin, ymax, ymax, ymin, ymin])
1987+
self.to_draw.set_data([xmin, xmax], [ymin, ymax])
19841988

19851989
def _set_active_handle(self, event):
19861990
"""Set active handle based on the location of the mouse event"""
@@ -2070,7 +2074,7 @@ def draw_shape(self, extents):
20702074
a = (xmax - xmin) / 2.
20712075
b = (ymax - ymin) / 2.
20722076

2073-
if self.drawtype == 'patch':
2077+
if self.drawtype == 'box':
20742078
self.to_draw.center = center
20752079
self.to_draw.width = 2 * a
20762080
self.to_draw.height = 2 * b
@@ -2082,7 +2086,7 @@ def draw_shape(self, extents):
20822086

20832087
@property
20842088
def _rect_bbox(self):
2085-
if self.drawtype == 'patch':
2089+
if self.drawtype == 'box':
20862090
x, y = self.to_draw.center
20872091
width = self.to_draw.width
20882092
height = self.to_draw.height

0 commit comments

Comments
 (0)