SF.net SVN: matplotlib:[8101] trunk/matplotlib

2010-01-29 Thread jdh2358
Revision: 8101
  http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8101&view=rev
Author:   jdh2358
Date: 2010-01-29 15:27:54 + (Fri, 29 Jan 2010)

Log Message:
---
added state to legend toggle

Modified Paths:
--
trunk/matplotlib/CHANGELOG
trunk/matplotlib/lib/matplotlib/legend.py

Modified: trunk/matplotlib/CHANGELOG
===
--- trunk/matplotlib/CHANGELOG  2010-01-28 21:46:12 UTC (rev 8100)
+++ trunk/matplotlib/CHANGELOG  2010-01-29 15:27:54 UTC (rev 8101)
@@ -1,3 +1,6 @@
+2010-01-29 Added draggable method to Legend to allow mouse drag
+placement.  Thanks Adam Fraser. JDH
+
 2010-01-25 Fixed a bug reported by Olle Engdegard, when using
histograms with stepfilled and log=True - MM
 

Modified: trunk/matplotlib/lib/matplotlib/legend.py
===
--- trunk/matplotlib/lib/matplotlib/legend.py   2010-01-28 21:46:12 UTC (rev 
8100)
+++ trunk/matplotlib/lib/matplotlib/legend.py   2010-01-29 15:27:54 UTC (rev 
8101)
@@ -935,16 +935,32 @@
 return ox, oy
 
 
-def draggable(self):
+def draggable(self, state=None):
 """
-toggle the draggable state; if on, you can drag the legend on
-the canvas.  The DraggableLegend helper class is returned
+Set the draggable state -- if state is
+
+  * None : toggle the current state
+
+  * True : turn draggable on
+
+  * False : turn draggable off
+  
+If draggable is on, you can drag the legend on the canvas with
+the mouse.  The DraggableLegend helper instance is returned if
+draggable is on.
 """
-if self._draggable is not None:
-self._draggable.disconnect()
+is_draggable = self._draggable is not None
+
+# if state is None we'll toggle
+if state is None:
+state = not is_draggable
+
+if state:
+if self._draggable is None:
+self._draggable = DraggableLegend(self)
+else:
+if self._draggable is not None:
+self._draggable.disconnect()
 self._draggable = None
-else:
 
-self._draggable = DraggableLegend(self)
-
 return self._draggable


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

--
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
___
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins


SF.net SVN: matplotlib:[8102] trunk/matplotlib/lib/matplotlib

2010-01-29 Thread leejjoon
Revision: 8102
  http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8102&view=rev
Author:   leejjoon
Date: 2010-01-29 16:22:51 + (Fri, 29 Jan 2010)

Log Message:
---
fix some issues in the bbox after the postscript distiller is run

Modified Paths:
--
trunk/matplotlib/lib/matplotlib/backends/backend_ps.py
trunk/matplotlib/lib/matplotlib/font_manager.py

Modified: trunk/matplotlib/lib/matplotlib/backends/backend_ps.py
===
--- trunk/matplotlib/lib/matplotlib/backends/backend_ps.py  2010-01-29 
15:27:54 UTC (rev 8101)
+++ trunk/matplotlib/lib/matplotlib/backends/backend_ps.py  2010-01-29 
16:22:51 UTC (rev 8102)
@@ -1383,14 +1383,17 @@
 This yields smaller files without illegal encapsulated postscript
 operators. The output is low-level, converting text to outlines.
 """
-paper = '-sPAPERSIZE=%s'% ptype
+
+paper_option = "-sPAPERSIZE=%s" % ptype
+
 psfile = tmpfile + '.ps'
 outfile = tmpfile + '.output'
 dpi = rcParams['ps.distiller.res']
 if sys.platform == 'win32': gs_exe = 'gswin32c'
 else: gs_exe = 'gs'
+
 command = '%s -dBATCH -dNOPAUSE -r%d -sDEVICE=pswrite %s -sOutputFile="%s" 
\
-"%s" > "%s"'% (gs_exe, dpi, paper, psfile, tmpfile, outfile)
+"%s" > "%s"'% (gs_exe, dpi, paper_option, psfile, tmpfile, 
outfile)
 verbose.report(command, 'debug')
 exit_status = os.system(command)
 fh = file(outfile)
@@ -1403,14 +1406,14 @@
 shutil.move(psfile, tmpfile)
 
 
+# While it is best if above steps preserve the original bounding
+# box, it does not seems to be the case. pstoeps not only convert
+# the input to eps format, but also restores the original bbox.
 
-# Since the the paper size is set to the figure size for eps
-# output (in '_print_figure_tex'), pstoeps call is not required.
+if eps:
+pstoeps(tmpfile, bbox)
 
-#if eps:
-#pstoeps(tmpfile, bbox)
 
-
 def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None):
 """
 Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file.
@@ -1421,9 +1424,13 @@
 pdffile = tmpfile + '.pdf'
 psfile = tmpfile + '.ps'
 outfile = tmpfile + '.output'
+
+if eps: paper_option = "-dEPSCrop"
+else: paper_option = "-sPAPERSIZE=%s" % ptype
+
 command = 'ps2pdf -dAutoFilterColorImages=false \
--sColorImageFilter=FlateEncode -sPAPERSIZE=%s "%s" "%s" > "%s"'% \
-(ptype, tmpfile, pdffile, outfile)
+-sColorImageFilter=FlateEncode %s "%s" "%s" > "%s"'% \
+(paper_option, tmpfile, pdffile, outfile)
 if sys.platform == 'win32': command = command.replace('=', '#')
 verbose.report(command, 'debug')
 exit_status = os.system(command)
@@ -1445,17 +1452,37 @@
 os.remove(outfile)
 os.remove(tmpfile)
 shutil.move(psfile, tmpfile)
+
+
+# Similar to the gs_distillier case, ps2pdf does not seem to
+# preserve the bbox of the original file (at least w/ gs
+# 8.61). Thus, the original bbox need to be resotred.
+
 if eps:
 pstoeps(tmpfile, bbox)
 for fname in glob.glob(tmpfile+'.*'):
 os.remove(fname)
 
 
+def get_bbox_header(l, b, r, t):
+"""
+return a postscript header stringfor the given bbox (l, b, r, t)
+"""
+
+bbox_info = 'BoundingBox: %d %d %d %d' % (l, b, npy.ceil(r), 
npy.ceil(t))
+hires_bbox_info = 'HiResBoundingBox: %.6f %.6f %.6f %.6f' % (l, b, r, 
t)
+
+return '\n'.join([bbox_info, hires_bbox_info])
+
+
+# get_bbox is deprecated. I don't see any reason to use ghostscript to
+# find the bounding box, as the required bounding box is alread known.
 def get_bbox(tmpfile, bbox):
 """
 Use ghostscript's bbox device to find the center of the bounding box. 
Return
 an appropriately sized bbox centered around that point. A bit of a hack.
 """
+
 outfile = tmpfile + '.output'
 if sys.platform == 'win32': gs_exe = 'gswin32c'
 else: gs_exe = 'gs'
@@ -1497,7 +1524,7 @@
 """
 Convert the postscript to encapsulated postscript.
 """
-bbox_info = get_bbox(tmpfile, bbox)
+bbox_info = get_bbox_header(*bbox)
 
 epsfile = tmpfile + '.eps'
 epsh = file(epsfile, 'w')
@@ -1552,6 +1579,7 @@
 shutil.move(epsfile, tmpfile)
 
 
+
 class FigureManagerPS(FigureManagerBase):
 pass
 

Modified: trunk/matplotlib/lib/matplotlib/font_manager.py
===
--- trunk/matplotlib/lib/matplotlib/font_manager.py 2010-01-29 15:27:54 UTC 
(rev 8101)
+++ trunk/matplotlib/lib/matplotlib/font_manager.py 2010-01-29 16:22:51 UTC 
(rev 8102)
@@ -1204,7 +1204,6 @@
 for font in fontlist:
 if (directory is not None and
 os.path.commonprefix([font.fname, directory]) != directory):
-print directory, font.fname, os.path.commonprefix([font.fname, 
directory])

SF.net SVN: matplotlib:[8103] trunk/matplotlib

2010-01-29 Thread leejjoon
Revision: 8103
  http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8103&view=rev
Author:   leejjoon
Date: 2010-01-29 17:33:21 + (Fri, 29 Jan 2010)

Log Message:
---
refactor draggable legend to support annotation

Modified Paths:
--
trunk/matplotlib/lib/matplotlib/legend.py
trunk/matplotlib/lib/matplotlib/offsetbox.py
trunk/matplotlib/lib/matplotlib/text.py

Added Paths:
---
trunk/matplotlib/examples/animation/draggable_legend.py

Added: trunk/matplotlib/examples/animation/draggable_legend.py
===
--- trunk/matplotlib/examples/animation/draggable_legend.py 
(rev 0)
+++ trunk/matplotlib/examples/animation/draggable_legend.py 2010-01-29 
17:33:21 UTC (rev 8103)
@@ -0,0 +1,43 @@
+import matplotlib.pyplot as plt
+
+
+ax = plt.subplot(111)
+ax.plot([1,2,3], label="test")
+
+l = ax.legend()
+d1 = l.draggable()
+
+xy = 1, 2
+txt = ax.annotate("Test", xy, xytext=(-30, 30),
+  textcoords="offset points",
+  bbox=dict(boxstyle="round",fc=(0.2, 1, 1)),
+  arrowprops=dict(arrowstyle="->"))
+d2 = txt.draggable()
+
+
+from matplotlib._png import read_png
+from matplotlib.cbook import get_sample_data
+
+from matplotlib.offsetbox import OffsetImage, AnnotationBbox
+
+fn = get_sample_data("lena.png", asfileobj=False)
+arr_lena = read_png(fn)
+
+imagebox = OffsetImage(arr_lena, zoom=0.2)
+
+ab = AnnotationBbox(imagebox, xy,
+xybox=(120., -80.),
+xycoords='data',
+boxcoords="offset points",
+pad=0.5,
+arrowprops=dict(arrowstyle="->",
+
connectionstyle="angle,angleA=0,angleB=90,rad=3")
+)
+
+
+ax.add_artist(ab)
+
+d3 = ab.draggable()
+
+
+plt.show()

Modified: trunk/matplotlib/lib/matplotlib/legend.py
===
--- trunk/matplotlib/lib/matplotlib/legend.py   2010-01-29 16:22:51 UTC (rev 
8102)
+++ trunk/matplotlib/lib/matplotlib/legend.py   2010-01-29 17:33:21 UTC (rev 
8103)
@@ -33,56 +33,28 @@
 from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
 from matplotlib.collections import LineCollection, RegularPolyCollection, \
  CircleCollection
-from matplotlib.transforms import Bbox, BboxBase, TransformedBbox, 
BboxTransformTo
+from matplotlib.transforms import Bbox, BboxBase, TransformedBbox, 
BboxTransformTo, BboxTransformFrom
 
-from matplotlib.offsetbox import HPacker, VPacker, TextArea, DrawingArea
+from matplotlib.offsetbox import HPacker, VPacker, TextArea, DrawingArea, 
DraggableOffsetBox
 
 
-class DraggableLegend:
-"""helper code for a draggable legend -- see Legend.draggable"""
-
+class DraggableLegend(DraggableOffsetBox):
 def __init__(self, legend):
-self.legend = legend
-self.gotLegend = False
+self.legend=legend
+DraggableOffsetBox.__init__(self, legend, legend._legend_box)
 
-c1 = legend.figure.canvas.mpl_connect('motion_notify_event', 
self.on_motion)
-c2 = legend.figure.canvas.mpl_connect('pick_event', self.on_pick)
-c3 = legend.figure.canvas.mpl_connect('button_release_event', 
self.on_release)
-legend.set_picker(self.my_legend_picker)
-self.cids = [c1, c2, c3]
-
-def on_motion(self, evt):
-if self.gotLegend:
-dx = evt.x - self.mouse_x
-dy = evt.y - self.mouse_y
-loc_in_canvas = self.legend_x + dx, self.legend_y + dy
-loc_in_norm_axes = 
self.legend.parent.transAxes.inverted().transform_point(loc_in_canvas)
-self.legend._loc = tuple(loc_in_norm_axes)
-self.legend.figure.canvas.draw()
-
-def my_legend_picker(self, legend, evt):
+def artist_picker(self, legend, evt):
 return self.legend.legendPatch.contains(evt)
 
-def on_pick(self, evt):
-legend = self.legend
-if evt.artist == legend:
-bbox = self.legend.get_window_extent()
-self.mouse_x = evt.mouseevent.x
-self.mouse_y = evt.mouseevent.y
-self.legend_x = bbox.xmin
-self.legend_y = bbox.ymin
-self.gotLegend = 1
+def finalize_offset(self):
+loc_in_canvas = self.get_loc_in_canvas()
 
-def on_release(self, event):
-if self.gotLegend:
-self.gotLegend = False
+bbox = self.legend.get_bbox_to_anchor()
+_bbox_transform = BboxTransformFrom(bbox)
+self.legend._loc = 
tuple(_bbox_transform.transform_point(loc_in_canvas))
+
 
-def disconnect(self):
-'disconnect the callbacks'
-for cid in self.cids:
-self.legend.figure.canvas.mpl_disconnect(cid)
 
-
 class Legend(Artist):
 """
 Place a legend on the axes at location loc.  Labels are a
@@ -323,7 +29

SF.net SVN: matplotlib:[8104] trunk/matplotlib/lib

2010-01-29 Thread leejjoon
Revision: 8104
  http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8104&view=rev
Author:   leejjoon
Date: 2010-01-29 17:33:34 + (Fri, 29 Jan 2010)

Log Message:
---
fix axes_grid for offsetbox api change

Modified Paths:
--
trunk/matplotlib/lib/matplotlib/offsetbox.py
trunk/matplotlib/lib/mpl_toolkits/axes_grid/inset_locator.py

Modified: trunk/matplotlib/lib/matplotlib/offsetbox.py
===
--- trunk/matplotlib/lib/matplotlib/offsetbox.py2010-01-29 17:33:21 UTC 
(rev 8103)
+++ trunk/matplotlib/lib/matplotlib/offsetbox.py2010-01-29 17:33:34 UTC 
(rev 8104)
@@ -958,7 +958,7 @@
 if fontsize is None:
 fontsize = 
renderer.points_to_pixels(self.prop.get_size_in_points())
 
-def _offset(w, h, xd, yd, fontsize=fontsize, self=self):
+def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self):
 bbox = Bbox.from_bounds(0, 0, w, h)
 borderpad = self.borderpad*fontsize
 bbox_to_anchor = self.get_bbox_to_anchor()

Modified: trunk/matplotlib/lib/mpl_toolkits/axes_grid/inset_locator.py
===
--- trunk/matplotlib/lib/mpl_toolkits/axes_grid/inset_locator.py
2010-01-29 17:33:21 UTC (rev 8103)
+++ trunk/matplotlib/lib/mpl_toolkits/axes_grid/inset_locator.py
2010-01-29 17:33:34 UTC (rev 8104)
@@ -47,7 +47,7 @@
 
width, height, xdescent, ydescent = self.get_extent(renderer)
 
-   px, py = self.get_offset(width, height, 0, 0)
+   px, py = self.get_offset(width, height, 0, 0, renderer)
bbox_canvas = mtrans.Bbox.from_bounds(px, py, width, height)
tr = ax.figure.transFigure.inverted()
bb = mtrans.TransformedBbox(bbox_canvas, tr)


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

--
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
___
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins


SF.net SVN: matplotlib:[8105] trunk/matplotlib

2010-01-29 Thread leejjoon
Revision: 8105
  http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8105&view=rev
Author:   leejjoon
Date: 2010-01-29 18:03:13 + (Fri, 29 Jan 2010)

Log Message:
---
draggable legend now optionally blitted

Modified Paths:
--
trunk/matplotlib/examples/animation/draggable_legend.py
trunk/matplotlib/lib/matplotlib/legend.py
trunk/matplotlib/lib/matplotlib/offsetbox.py
trunk/matplotlib/lib/matplotlib/text.py

Modified: trunk/matplotlib/examples/animation/draggable_legend.py
===
--- trunk/matplotlib/examples/animation/draggable_legend.py 2010-01-29 
17:33:34 UTC (rev 8104)
+++ trunk/matplotlib/examples/animation/draggable_legend.py 2010-01-29 
18:03:13 UTC (rev 8105)
@@ -37,7 +37,7 @@
 
 ax.add_artist(ab)
 
-d3 = ab.draggable()
+d3 = ab.draggable(use_blit=True)
 
 
 plt.show()

Modified: trunk/matplotlib/lib/matplotlib/legend.py
===
--- trunk/matplotlib/lib/matplotlib/legend.py   2010-01-29 17:33:34 UTC (rev 
8104)
+++ trunk/matplotlib/lib/matplotlib/legend.py   2010-01-29 18:03:13 UTC (rev 
8105)
@@ -39,9 +39,10 @@
 
 
 class DraggableLegend(DraggableOffsetBox):
-def __init__(self, legend):
+def __init__(self, legend, use_blit=False):
 self.legend=legend
-DraggableOffsetBox.__init__(self, legend, legend._legend_box)
+DraggableOffsetBox.__init__(self, legend, legend._legend_box,
+use_blit=use_blit)
 
 def artist_picker(self, legend, evt):
 return self.legend.legendPatch.contains(evt)
@@ -917,7 +918,7 @@
 return ox, oy
 
 
-def draggable(self, state=None):
+def draggable(self, state=None, use_blit=False):
 """
 Set the draggable state -- if state is
 
@@ -939,7 +940,7 @@
 
 if state:
 if self._draggable is None:
-self._draggable = DraggableLegend(self)
+self._draggable = DraggableLegend(self, use_blit)
 else:
 if self._draggable is not None:
 self._draggable.disconnect()

Modified: trunk/matplotlib/lib/matplotlib/offsetbox.py
===
--- trunk/matplotlib/lib/matplotlib/offsetbox.py2010-01-29 17:33:34 UTC 
(rev 8104)
+++ trunk/matplotlib/lib/matplotlib/offsetbox.py2010-01-29 18:03:13 UTC 
(rev 8105)
@@ -1416,10 +1416,11 @@
  the normalized axes coordinate and set a relavant attribute.
 
 """
-def __init__(self, ref_artist):
+def __init__(self, ref_artist, use_blit=False):
 self.ref_artist = ref_artist
 self.got_artist = False
-
+self._use_blit = use_blit
+
 self.canvas = self.ref_artist.figure.canvas
 c2 = self.canvas.mpl_connect('pick_event', self.on_pick)
 c3 = self.canvas.mpl_connect('button_release_event', self.on_release)
@@ -1432,16 +1433,34 @@
 dx = evt.x - self.mouse_x
 dy = evt.y - self.mouse_y
 self.update_offset(dx, dy)
+self.canvas.draw()
 
+def on_motion_blit(self, evt):
+if self.got_artist:
+dx = evt.x - self.mouse_x
+dy = evt.y - self.mouse_y
+self.update_offset(dx, dy)
+self.canvas.restore_region(self.background)
+self.ref_artist.draw(self.ref_artist.figure._cachedRenderer)
+self.canvas.blit(self.ref_artist.figure.bbox)
+
 def on_pick(self, evt):
 if evt.artist == self.ref_artist:
 
-self.save_offset()
 self.mouse_x = evt.mouseevent.x
 self.mouse_y = evt.mouseevent.y
 self.got_artist = True
 
-self._c1 = self.canvas.mpl_connect('motion_notify_event', 
self.on_motion)
+if self._use_blit:
+self.ref_artist.set_animated(True)
+self.canvas.draw()
+self.background = 
self.canvas.copy_from_bbox(self.ref_artist.figure.bbox)
+self.ref_artist.draw(self.ref_artist.figure._cachedRenderer)
+self.canvas.blit(self.ref_artist.figure.bbox)
+self._c1 = self.canvas.mpl_connect('motion_notify_event', 
self.on_motion_blit)
+else:
+self._c1 = self.canvas.mpl_connect('motion_notify_event', 
self.on_motion)
+self.save_offset()
 
 def on_release(self, event):
 if self.got_artist:
@@ -1449,6 +1468,9 @@
 self.got_artist = False
 self.canvas.mpl_disconnect(self._c1)
 
+if self._use_blit:
+self.ref_artist.set_animated(False)
+
 def disconnect(self):
 'disconnect the callbacks'
 for cid in self.cids:
@@ -1468,8 +1490,8 @@
 
 
 class DraggableOffsetBox(DraggableBase):
-def __init__(self, ref_artist, offsetbox):
-DraggableBase.__init__(self