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

Skip to content

Lasso selector #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 17, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add AxesWidget class and let it initialize ax and canvas attributes.
* Renamed `Lasso.axes` to `Lasso.ax` for consistency.
* Remove `Lasso.figure`, which was unused.
  • Loading branch information
tonysyu committed Feb 27, 2012
commit e37c135cc515f267318b9528c6670b2e3ab6197c
74 changes: 41 additions & 33 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,23 @@ class Widget(object):
drawon = True
eventson = True

class AxesWidget(Widget):
"""
Widget that is connected to a single :class:`Axes`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this render correctly? It is certainly safer to do :class:~matplotlib.axes.Axes. (the tilde shortens the link to Axes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about this. Thanks for the tip.


Attributes
----------
*ax*
The parent :class:`matplotlib.axes.Axes` for the widget
*canvas*
The parent FigureCanvas for the widget
"""
def __init__(self, ax):
self.ax = ax
self.canvas = ax.figure.canvas



class Button(Widget):
class Button(AxesWidget):
"""
A GUI neutral button

Expand Down Expand Up @@ -108,6 +121,8 @@ def __init__(self, ax, label, image=None,
*hovercolor*
The color of the button when the mouse is over it
"""
AxesWidget.__init__(self, ax)

if image is not None:
ax.imshow(image)
self.label = ax.text(0.5, 0.5, label,
Expand All @@ -117,8 +132,6 @@ def __init__(self, ax, label, image=None,

self.cnt = 0
self.observers = {}
self.ax = ax


ax.figure.canvas.mpl_connect('button_press_event', self._click)
ax.figure.canvas.mpl_connect('button_release_event', self._release)
Expand Down Expand Up @@ -179,7 +192,7 @@ def disconnect(self, cid):



class Slider(Widget):
class Slider(AxesWidget):
"""
A slider representing a floating point range

Expand Down Expand Up @@ -240,7 +253,7 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
knob. See the :class:`matplotlib.patches.Rectangle` documentation
valid property names (e.g., *facecolor*, *edgecolor*, *alpha*, ...)
"""
self.ax = ax
AxesWidget.__init__(self, ax)

self.valmin = valmin
self.valmax = valmax
Expand Down Expand Up @@ -351,7 +364,7 @@ def reset(self):



class CheckButtons(Widget):
class CheckButtons(AxesWidget):
"""
A GUI neutral radio button

Expand Down Expand Up @@ -385,6 +398,7 @@ def __init__(self, ax, labels, actives):
A len(buttons) list of booleans indicating whether
the button is active
"""
AxesWidget.__init__(self, ax)

ax.set_xticks([])
ax.set_yticks([])
Expand Down Expand Up @@ -433,8 +447,6 @@ def __init__(self, ax, labels, actives):
cnt += 1

ax.figure.canvas.mpl_connect('button_press_event', self._clicked)
self.ax = ax


self.cnt = 0
self.observers = {}
Expand Down Expand Up @@ -479,7 +491,7 @@ def disconnect(self, cid):
except KeyError: pass


class RadioButtons(Widget):
class RadioButtons(AxesWidget):
"""
A GUI neutral radio button

Expand Down Expand Up @@ -512,8 +524,9 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
*activecolor*
The color of the button when clicked
"""
self.activecolor = activecolor
AxesWidget.__init__(self, ax)

self.activecolor = activecolor

ax.set_xticks([])
ax.set_yticks([])
Expand Down Expand Up @@ -545,8 +558,6 @@ def __init__(self, ax, labels, active=0, activecolor='blue'):
cnt += 1

ax.figure.canvas.mpl_connect('button_press_event', self._clicked)
self.ax = ax


self.cnt = 0
self.observers = {}
Expand Down Expand Up @@ -734,7 +745,7 @@ def funchspace(self, val):
if self.drawon: self.targetfig.canvas.draw()


class Cursor(Widget):
class Cursor(AxesWidget):
"""
A horizontal and vertical line span the axes that and move with
the pointer. You can turn off the hline or vline spectively with
Expand All @@ -757,9 +768,7 @@ def __init__(self, ax, useblit=False, **lineprops):
.. plot :: mpl_examples/widgets/cursor.py
"""
# TODO: Is the GTKAgg limitation still true?

self.ax = ax
self.canvas = ax.figure.canvas
AxesWidget.__init__(self, ax)

self.canvas.mpl_connect('motion_notify_event', self.onmove)
self.canvas.mpl_connect('draw_event', self.clear)
Expand Down Expand Up @@ -895,7 +904,7 @@ def _update(self):

self.canvas.draw_idle()

class SpanSelector(Widget):
class SpanSelector(AxesWidget):
"""
Select a min/max range of the x or y axes for a matplotlib Axes

Expand Down Expand Up @@ -933,14 +942,14 @@ def __init__(self, ax, onselect, direction, minspan=None, useblit=False,
Set the visible attribute to ``False`` if you want to turn off
the functionality of the span selector
"""
AxesWidget.__init__(self, ax)

if rectprops is None:
rectprops = dict(facecolor='red', alpha=0.5)

assert direction in ['horizontal', 'vertical'], 'Must choose horizontal or vertical for direction'
self.direction = direction

self.ax = ax
self.canvas = ax.figure.canvas
self.visible = True
self.cids=[]

Expand Down Expand Up @@ -1101,7 +1110,7 @@ def __init__(self, ax, onselect, **kwargs):
SpanSelector.__init__(self, ax, onselect, 'horizontal', **kwargs)


class RectangleSelector(Widget):
class RectangleSelector(AxesWidget):
"""
Select a min/max range of the x axes for a matplotlib Axes

Expand Down Expand Up @@ -1181,9 +1190,9 @@ def __init__(self, ax, onselect, drawtype='box',
2 = center mouse button (scroll wheel)
3 = right mouse button
"""
self.ax = ax
AxesWidget.__init__(self, ax)

self.visible = True
self.canvas = ax.figure.canvas
self.canvas.mpl_connect('motion_notify_event', self.onmove)
self.canvas.mpl_connect('button_press_event', self.press)
self.canvas.mpl_connect('button_release_event', self.release)
Expand Down Expand Up @@ -1378,19 +1387,18 @@ def get_active(self):
""" Get status of active mode (boolean variable)"""
return self.active

class Lasso(Widget):
class Lasso(AxesWidget):
def __init__(self, ax, xy, callback=None, useblit=True):
self.axes = ax
self.figure = ax.figure
self.canvas = self.figure.canvas
AxesWidget.__init__(self, ax)

self.useblit = useblit
if useblit:
self.background = self.canvas.copy_from_bbox(self.axes.bbox)
self.background = self.canvas.copy_from_bbox(self.ax.bbox)

x, y = xy
self.verts = [(x,y)]
self.line = Line2D([x], [y], linestyle='-', color='black', lw=2)
self.axes.add_line(self.line)
self.ax.add_line(self.line)
self.callback = callback
self.cids = []
self.cids.append(self.canvas.mpl_connect('button_release_event', self.onrelease))
Expand All @@ -1401,22 +1409,22 @@ def onrelease(self, event):
self.verts.append((event.xdata, event.ydata))
if len(self.verts)>2:
self.callback(self.verts)
self.axes.lines.remove(self.line)
self.ax.lines.remove(self.line)
self.verts = None
for cid in self.cids:
self.canvas.mpl_disconnect(cid)

def onmove(self, event):
if self.verts is None: return
if event.inaxes != self.axes: return
if event.inaxes != self.ax: return
if event.button!=1: return
self.verts.append((event.xdata, event.ydata))

self.line.set_data(zip(*self.verts))

if self.useblit:
self.canvas.restore_region(self.background)
self.axes.draw_artist(self.line)
self.canvas.blit(self.axes.bbox)
self.ax.draw_artist(self.line)
self.canvas.blit(self.ax.bbox)
else:
self.canvas.draw_idle()