From: <jd...@us...> - 2008-07-14 15:14:13
|
Revision: 5769 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5769&view=rev Author: jdh2358 Date: 2008-07-14 08:14:02 -0700 (Mon, 14 Jul 2008) Log Message: ----------- added support for pixels or data min coords to the rectangle selector widget Modified Paths: -------------- trunk/matplotlib/doc/faq/howto_faq.rst trunk/matplotlib/examples/widgets/rectangle_selector.py trunk/matplotlib/lib/matplotlib/artist.py trunk/matplotlib/lib/matplotlib/pyplot.py trunk/matplotlib/lib/matplotlib/widgets.py Modified: trunk/matplotlib/doc/faq/howto_faq.rst =================================================================== --- trunk/matplotlib/doc/faq/howto_faq.rst 2008-07-14 12:54:59 UTC (rev 5768) +++ trunk/matplotlib/doc/faq/howto_faq.rst 2008-07-14 15:14:02 UTC (rev 5769) @@ -6,6 +6,33 @@ .. contents:: + +.. _howto-findobj: + +How do I find all the objects in my figure of a certain type? +============================================================= + +Every matplotlib artist (see :ref:`artist-tutorial`) has a method +called :meth:`~matplotlib.artist.Artist.findobj` that can be used to +recursively search the artist for any artists it may contain that meet +some criteria (eg match all :class:`~matplotlib.lines.Line2D` +instances or match some arbitrary filter function). For example, the +following snippet finds every object in the figure which has a +`set_color` property and makes the object blue:: + + def myfunc(x): + return hasattr(x, 'set_color') + + for o in fig.findobj(myfunc): + o.set_color('blue') + +You can also filter on class instances:: + + import matplotlib.text as text + for o in fig.findobj(text.Text): + o.set_fontstyle('italic') + + .. _howto-transparent: How do I save transparent figures? Modified: trunk/matplotlib/examples/widgets/rectangle_selector.py =================================================================== --- trunk/matplotlib/examples/widgets/rectangle_selector.py 2008-07-14 12:54:59 UTC (rev 5768) +++ trunk/matplotlib/examples/widgets/rectangle_selector.py 2008-07-14 15:14:02 UTC (rev 5769) @@ -29,5 +29,6 @@ # drawtype is 'box' or 'line' or 'none' LS = RectangleSelector(current_ax, line_select_callback, - drawtype='box',useblit=True) + drawtype='box',useblit=True, + minspanx=5,minspany=5,spancoords='pixels') show() Modified: trunk/matplotlib/lib/matplotlib/artist.py =================================================================== --- trunk/matplotlib/lib/matplotlib/artist.py 2008-07-14 12:54:59 UTC (rev 5768) +++ trunk/matplotlib/lib/matplotlib/artist.py 2008-07-14 15:14:02 UTC (rev 5769) @@ -510,6 +510,9 @@ def findobj(self, match=None): """ + pyplot signature: + findobj(o=gcf(), match=None) + recursively find all :class:matplotlib.artist.Artist instances contained in self @@ -520,6 +523,8 @@ - function with signature ``boolean = match(artist)`` used to filter matches - class instance: eg Line2D. Only return artists of class type + + .. plot:: ../mpl_examples/pylab_examples/findobj_demo.py """ if match is None: # always return True Modified: trunk/matplotlib/lib/matplotlib/pyplot.py =================================================================== --- trunk/matplotlib/lib/matplotlib/pyplot.py 2008-07-14 12:54:59 UTC (rev 5768) +++ trunk/matplotlib/lib/matplotlib/pyplot.py 2008-07-14 15:14:02 UTC (rev 5769) @@ -7,7 +7,7 @@ from matplotlib.backend_bases import FigureCanvasBase from matplotlib.image import imread as _imread from matplotlib import rcParams, rcParamsDefault, get_backend -from matplotlib.artist import getp, get +from matplotlib.artist import getp, get, Artist from matplotlib.artist import setp as _setp from matplotlib.axes import Axes from matplotlib.projections import PolarAxes @@ -41,26 +41,11 @@ def findobj(o=None, match=None): - """ - recursively find all :class:matplotlib.artist.Artist instances - contained in artist instance *p*. if *o* is None, use - current figure - - *match* can be - - - None: return all objects contained in artist (including artist) - - - function with signature ``boolean = match(artist)`` used to filter matches - - - class instance: eg Line2D. Only return artists of class type - - """ - if o is None: o = gcf() return o.findobj(match) +findobj.__doc__ = Artist.findobj.__doc__ - def switch_backend(newbackend): """ Switch the default backend to newbackend. This feature is Modified: trunk/matplotlib/lib/matplotlib/widgets.py =================================================================== --- trunk/matplotlib/lib/matplotlib/widgets.py 2008-07-14 12:54:59 UTC (rev 5768) +++ trunk/matplotlib/lib/matplotlib/widgets.py 2008-07-14 15:14:02 UTC (rev 5769) @@ -979,7 +979,7 @@ """ Select a min/max range of the x axes for a matplotlib Axes - Example usage: + Example usage:: from matplotlib.widgets import RectangleSelector from pylab import * @@ -1011,7 +1011,7 @@ """ def __init__(self, ax, onselect, drawtype='box', minspanx=None, minspany=None, useblit=False, - lineprops=None, rectprops=None): + lineprops=None, rectprops=None, spancoords='data'): """ Create a selector in ax. When a selection is made, clear @@ -1035,7 +1035,12 @@ Use type if you want the mouse to draw a line, a box or nothing between click and actual position ny setting + drawtype = 'line', drawtype='box' or drawtype = 'none'. + + spancoords is one of 'data' or 'pixels'. If 'data', minspanx + and minspanx will be interpreted in the same coordinates as + the x and ya axis, if 'pixels', they are in pixels """ self.ax = ax self.visible = True @@ -1072,6 +1077,10 @@ self.useblit = useblit self.minspanx = minspanx self.minspany = minspany + + assert(spancoords in ('data', 'pixels')) + + self.spancoords = spancoords self.drawtype = drawtype # will save the data (position at mouseclick) self.eventpress = None @@ -1125,15 +1134,22 @@ self.canvas.draw() # release coordinates, button, ... self.eventrelease = event - xmin, ymin = self.eventpress.xdata, self.eventpress.ydata - xmax, ymax = self.eventrelease.xdata, self.eventrelease.ydata - # calculate dimensions of box or line get values in the right - # order + + if self.spancoords=='data': + xmin, ymin = self.eventpress.xdata, self.eventpress.ydata + xmax, ymax = self.eventrelease.xdata, self.eventrelease.ydata + # calculate dimensions of box or line get values in the right + # order + elif self.spancoords=='pixels': + xmin, ymin = self.eventpress.x, self.eventpress.y + xmax, ymax = self.eventrelease.x, self.eventrelease.y + else: + raise ValueError('spancoords must be "data" or "pixels"') + + if xmin>xmax: xmin, xmax = xmax, xmin if ymin>ymax: ymin, ymax = ymax, ymin - - spanx = xmax - xmin spany = ymax - ymin xproblems = self.minspanx is not None and spanx<self.minspanx This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |