From: <ef...@us...> - 2010-06-01 18:21:36
|
Revision: 8362 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8362&view=rev Author: efiring Date: 2010-06-01 18:21:30 +0000 (Tue, 01 Jun 2010) Log Message: ----------- widget example improvements by M. Michler, patch 2944605 Modified Paths: -------------- trunk/matplotlib/examples/widgets/buttons.py trunk/matplotlib/examples/widgets/check_buttons.py trunk/matplotlib/examples/widgets/cursor.py trunk/matplotlib/examples/widgets/menu.py trunk/matplotlib/examples/widgets/rectangle_selector.py Modified: trunk/matplotlib/examples/widgets/buttons.py =================================================================== --- trunk/matplotlib/examples/widgets/buttons.py 2010-06-01 16:38:34 UTC (rev 8361) +++ trunk/matplotlib/examples/widgets/buttons.py 2010-06-01 18:21:30 UTC (rev 8362) @@ -1,38 +1,40 @@ -from pylab import * + +import numpy as np +import matplotlib.pyplot as plt from matplotlib.widgets import Button -freqs = arange(2,20,3) +freqs = np.arange(2, 20, 3) -ax = subplot(111) -subplots_adjust(bottom=0.2) -t = arange(0.0, 1.0, 0.001) -s = sin(2*pi*freqs[0]*t) -l, = plot(t,s, lw=2) +ax = plt.subplot(111) +plt.subplots_adjust(bottom=0.2) +t = np.arange(0.0, 1.0, 0.001) +s = np.sin(2*np.pi*freqs[0]*t) +l, = plt.plot(t, s, lw=2) class Index: ind = 0 def next(self, event): self.ind += 1 - i = self.ind%len(freqs) - ydata = sin(2*pi*freqs[i]*t) + i = self.ind % len(freqs) + ydata = np.sin(2*np.pi*freqs[i]*t) l.set_ydata(ydata) - draw() + plt.draw() def prev(self, event): self.ind -= 1 - i = self.ind%len(freqs) - ydata = sin(2*pi*freqs[i]*t) + i = self.ind % len(freqs) + ydata = np.sin(2*np.pi*freqs[i]*t) l.set_ydata(ydata) - draw() + plt.draw() callback = Index() -axprev = axes([0.7, 0.05, 0.1, 0.075]) -axnext = axes([0.81, 0.05, 0.1, 0.075]) +axprev = plt.axes([0.7, 0.05, 0.1, 0.075]) +axnext = plt.axes([0.81, 0.05, 0.1, 0.075]) bnext = Button(axnext, 'Next') bnext.on_clicked(callback.next) bprev = Button(axprev, 'Previous') bprev.on_clicked(callback.prev) -show() +plt.show() Modified: trunk/matplotlib/examples/widgets/check_buttons.py =================================================================== --- trunk/matplotlib/examples/widgets/check_buttons.py 2010-06-01 16:38:34 UTC (rev 8361) +++ trunk/matplotlib/examples/widgets/check_buttons.py 2010-06-01 18:21:30 UTC (rev 8362) @@ -1,25 +1,26 @@ -from pylab import * +import numpy as np +import matplotlib.pyplot as plt from matplotlib.widgets import CheckButtons -t = arange(0.0, 2.0, 0.01) -s0 = sin(2*pi*t) -s1 = sin(4*pi*t) -s2 = sin(6*pi*t) +t = np.arange(0.0, 2.0, 0.01) +s0 = np.sin(2*np.pi*t) +s1 = np.sin(4*np.pi*t) +s2 = np.sin(6*np.pi*t) -ax = subplot(111) +ax = plt.subplot(111) l0, = ax.plot(t, s0, visible=False, lw=2) l1, = ax.plot(t, s1, lw=2) l2, = ax.plot(t, s2, lw=2) -subplots_adjust(left=0.2) +plt.subplots_adjust(left=0.2) -rax = axes([0.05, 0.4, 0.1, 0.15]) +rax = plt.axes([0.05, 0.4, 0.1, 0.15]) check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (False, True, True)) def func(label): - if label=='2 Hz': l0.set_visible(not l0.get_visible()) - elif label=='4 Hz': l1.set_visible(not l1.get_visible()) - elif label=='6 Hz': l2.set_visible(not l2.get_visible()) - draw() + if label == '2 Hz': l0.set_visible(not l0.get_visible()) + elif label == '4 Hz': l1.set_visible(not l1.get_visible()) + elif label == '6 Hz': l2.set_visible(not l2.get_visible()) + plt.draw() check.on_clicked(func) -show() +plt.show() Modified: trunk/matplotlib/examples/widgets/cursor.py =================================================================== --- trunk/matplotlib/examples/widgets/cursor.py 2010-06-01 16:38:34 UTC (rev 8361) +++ trunk/matplotlib/examples/widgets/cursor.py 2010-06-01 18:21:30 UTC (rev 8362) @@ -1,20 +1,19 @@ #!/usr/bin/env python from matplotlib.widgets import Cursor -import pylab +import numpy as np +import matplotlib.pyplot as plt -fig = pylab.figure(figsize=(8,6)) -ax = fig.add_axes([0.075, 0.25, 0.9, 0.725], axisbg='#FFFFCC') -#ax = fig.add_subplot(111, axisbg='#FFFFCC') -canvas = ax.figure.canvas +fig = plt.figure(figsize=(8, 6)) +ax = fig.add_subplot(111, axisbg='#FFFFCC') -x,y = 4*(pylab.rand(2,100)-.5) -ax.plot(x,y,'o') -ax.set_xlim(-2,2) -ax.set_ylim(-2,2) +x, y = 4*(np.random.rand(2, 100)-.5) +ax.plot(x, y, 'o') +ax.set_xlim(-2, 2) +ax.set_ylim(-2, 2) # set useblit = True on gtkagg for enhanced performance cursor = Cursor(ax, useblit=True, color='red', linewidth=2 ) -pylab.show() +plt.show() Modified: trunk/matplotlib/examples/widgets/menu.py =================================================================== --- trunk/matplotlib/examples/widgets/menu.py 2010-06-01 16:38:34 UTC (rev 8361) +++ trunk/matplotlib/examples/widgets/menu.py 2010-06-01 18:21:30 UTC (rev 8362) @@ -9,7 +9,8 @@ class ItemProperties: - def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow', alpha=1.0): + def __init__(self, fontsize=14, labelcolor='black', bgcolor='yellow', + alpha=1.0): self.fontsize = fontsize self.labelcolor = labelcolor self.bgcolor = bgcolor @@ -21,8 +22,9 @@ class MenuItem(artist.Artist): parser = mathtext.MathTextParser("Bitmap") padx = 5 - pady =5 - def __init__(self, fig, labelstr, props=None, hoverprops=None, on_select=None): + pady = 5 + def __init__(self, fig, labelstr, props=None, hoverprops=None, + on_select=None): artist.Artist.__init__(self) self.set_figure(fig) @@ -44,14 +46,15 @@ labelstr, fontsize=props.fontsize, dpi=fig.dpi) if props.fontsize!=hoverprops.fontsize: - raise NotImplementedError('support for different font sizes not implemented') + raise NotImplementedError( + 'support for different font sizes not implemented') self.labelwidth = x.shape[1] self.labelheight = x.shape[0] self.labelArray = np.zeros((x.shape[0], x.shape[1], 4)) - self.labelArray[:,:,-1] = x/255. + self.labelArray[:, :, -1] = x/255. self.label = image.FigureImage(fig, origin='upper') self.label.set_array(self.labelArray) @@ -95,9 +98,9 @@ props = self.props r, g, b = props.labelcolor_rgb - self.labelArray[:,:,0] = r - self.labelArray[:,:,1] = g - self.labelArray[:,:,2] = b + self.labelArray[:, :, 0] = r + self.labelArray[:, :, 1] = g + self.labelArray[:, :, 2] = b self.label.set_array(self.labelArray) self.rect.set(facecolor=props.bgcolor, alpha=props.alpha) @@ -169,12 +172,9 @@ for label in ('open', 'close', 'save', 'save as', 'quit'): def on_select(item): print 'you selected', item.labelstr - item = MenuItem(fig, label, props=props, hoverprops=hoverprops, on_select=on_select) + item = MenuItem(fig, label, props=props, hoverprops=hoverprops, + on_select=on_select) menuitems.append(item) menu = Menu(fig, menuitems) plt.show() - - - - Modified: trunk/matplotlib/examples/widgets/rectangle_selector.py =================================================================== --- trunk/matplotlib/examples/widgets/rectangle_selector.py 2010-06-01 16:38:34 UTC (rev 8361) +++ trunk/matplotlib/examples/widgets/rectangle_selector.py 2010-06-01 18:21:30 UTC (rev 8362) @@ -8,28 +8,41 @@ """ from matplotlib.widgets import RectangleSelector -from pylab import subplot, arange, plot, sin, cos, pi, show -def line_select_callback(event1, event2): - 'event1 and event2 are the press and release events' - x1, y1 = event1.xdata, event1.ydata - x2, y2 = event2.xdata, event2.ydata - print "(%3.2f, %3.2f) --> (%3.2f, %3.2f)"%(x1,y1,x2,y2) - print " The button you used were: ",event1.button, event2.button +import numpy as np +import matplotlib.pyplot as plt +def line_select_callback(eclick, erelease): + 'eclick and erelease are the press and release events' + x1, y1 = eclick.xdata, eclick.ydata + x2, y2 = erelease.xdata, erelease.ydata + print "(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2) + print " The button you used were: ", eclick.button, erelease.button -current_ax=subplot(111) # make a new plotingrange -N=100000 # If N is large one can see improvement -x=10.0*arange(N)/(N-1) # by use blitting! +def toggle_selector(event): + print ' Key pressed.' + if event.key in ['Q', 'q'] and toggle_selector.RS.active: + print ' RectangleSelector deactivated.' + toggle_selector.RS.set_active(False) + if event.key in ['A', 'a'] and not toggle_selector.RS.active: + print ' RectangleSelector activated.' + toggle_selector.RS.set_active(True) -plot(x,sin(.2*pi*x),lw=3,c='b',alpha=.7) # plot something -plot(x,cos(.2*pi*x),lw=3.5,c='r',alpha=.5) -plot(x,-sin(.2*pi*x),lw=3.5,c='g',alpha=.3) +current_ax = plt.subplot(111) # make a new plotingrange +N = 100000 # If N is large one can see +x = np.linspace(0.0, 10.0, N) # improvement by use blitting! + +plt.plot(x, +np.sin(.2*np.pi*x), lw=3.5, c='b', alpha=.7) # plot something +plt.plot(x, +np.cos(.2*np.pi*x), lw=3.5, c='r', alpha=.5) +plt.plot(x, -np.sin(.2*np.pi*x), lw=3.5, c='g', alpha=.3) + print "\n click --> release" # drawtype is 'box' or 'line' or 'none' -LS = RectangleSelector(current_ax, line_select_callback, - drawtype='box',useblit=True, - button = [1, 3], # don't use center mouse button - minspanx=5,minspany=5,spancoords='pixels') -show() +toggle_selector.RS = RectangleSelector(current_ax, line_select_callback, + drawtype='box', useblit=True, + button=[1,3], # don't use middle button + minspanx=5, minspany=5, + spancoords='pixels') +plt.connect('key_press_event', toggle_selector) +plt.show() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |