Revision: 8852
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8852&view=rev
Author:   leejjoon
Date:     2010-12-29 08:29:59 +0000 (Wed, 29 Dec 2010)

Log Message:
-----------
mpl_tollkits.axes_grid1 support auto-adjusted axes area

Modified Paths:
--------------
    trunk/matplotlib/lib/matplotlib/axes.py
    trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_divider.py
    trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_size.py

Added Paths:
-----------
    trunk/matplotlib/examples/axes_grid/make_room_for_ylabel_using_axesgrid.py

Added: 
trunk/matplotlib/examples/axes_grid/make_room_for_ylabel_using_axesgrid.py
===================================================================
--- trunk/matplotlib/examples/axes_grid/make_room_for_ylabel_using_axesgrid.py  
                        (rev 0)
+++ trunk/matplotlib/examples/axes_grid/make_room_for_ylabel_using_axesgrid.py  
2010-12-29 08:29:59 UTC (rev 8852)
@@ -0,0 +1,62 @@
+from mpl_toolkits.axes_grid1 import make_axes_locatable
+from mpl_toolkits.axes_grid1.axes_divider import make_axes_area_auto_adjustable
+
+
+
+if __name__ == "__main__":
+    
+    import matplotlib.pyplot as plt
+    def ex1():
+        plt.figure(1)
+        ax = plt.axes([0,0,1,1])
+        # ax = plt.subplot(111)
+
+        ax.set_yticks([0.5])
+        ax.set_yticklabels(["very long label"])
+
+        make_axes_area_auto_adjustable(ax)
+    
+
+    def ex2():
+
+        plt.figure(2)
+        ax1 = plt.axes([0,0,1,0.5])
+        ax2 = plt.axes([0,0.5,1,0.5])
+
+        ax1.set_yticks([0.5])
+        ax1.set_yticklabels(["very long label"])
+        ax1.set_ylabel("Y label")
+        
+        ax2.set_title("Title")
+
+        make_axes_area_auto_adjustable(ax1, pad=0.1, use_axes=[ax1, ax2]) 
+        make_axes_area_auto_adjustable(ax2, pad=0.1, use_axes=[ax1, ax2])
+
+    def ex3():
+
+        fig = plt.figure(3)
+        ax1 = plt.axes([0,0,1,1])
+        divider = make_axes_locatable(ax1)
+        
+        ax2 = divider.new_horizontal("100%", pad=0.3, sharey=ax1)
+        ax2.tick_params(labelleft="off")
+        fig.add_axes(ax2)
+
+        divider.add_auto_adjustable_area(use_axes=[ax1], pad=0.1,
+                                         adjust_dirs=["left"])
+        divider.add_auto_adjustable_area(use_axes=[ax2], pad=0.1,
+                                         adjust_dirs=["right"])
+        divider.add_auto_adjustable_area(use_axes=[ax1, ax2], pad=0.1,
+                                         adjust_dirs=["top", "bottom"])
+    
+        ax1.set_yticks([0.5])
+        ax1.set_yticklabels(["very long label"])
+
+        ax2.set_title("Title")
+        ax2.set_xlabel("X - Label")
+    
+    ex1()
+    ex2()
+    ex3()
+
+    plt.show()

Modified: trunk/matplotlib/lib/matplotlib/axes.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/axes.py     2010-12-29 08:29:11 UTC (rev 
8851)
+++ trunk/matplotlib/lib/matplotlib/axes.py     2010-12-29 08:29:59 UTC (rev 
8852)
@@ -8293,10 +8293,12 @@
         if self.xaxis.get_visible():
             artists.append(self.xaxis.label)
             bbx1, bbx2 = self.xaxis.get_ticklabel_extents(renderer)
+            self.xaxis._update_label_position([bbx1], [bbx2])
             bb.extend([bbx1, bbx2])
         if self.yaxis.get_visible():
             artists.append(self.yaxis.label)
             bby1, bby2 = self.yaxis.get_ticklabel_extents(renderer)
+            self.yaxis._update_label_position([bby1], [bby2])
             bb.extend([bby1, bby2])
 
 

Modified: trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_divider.py
===================================================================
--- trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_divider.py        
2010-12-29 08:29:11 UTC (rev 8851)
+++ trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_divider.py        
2010-12-29 08:29:59 UTC (rev 8852)
@@ -260,8 +260,34 @@
         """
         return AxesLocator(self, nx, ny, nx1, ny1)
 
+    def append_size(self, position, size):
 
+        if position == "left":
+            self._horizontal.insert(0, size)
+            self._xrefindex += 1
+        elif position == "right":
+            self._horizontal.append(size)
+        elif position == "bottom":
+            self._vertical.insert(0, size)
+            self._yrefindex += 1
+        elif position == "top":
+            self._vertical.append(size)
+        else:
+            raise ValueError("the position must be one of left, right, bottom, 
or top")
 
+
+    def add_auto_adjustable_area(self, 
+                                 use_axes, pad=0.1,
+                                 adjust_dirs=["left", "right", "bottom", 
"top"],
+                                 ):
+        from axes_size import Padded, SizeFromFunc, GetExtentHelper
+        for d in adjust_dirs:
+            helper = GetExtentHelper(use_axes, d)
+            size = SizeFromFunc(helper)
+            padded_size = Padded(size, pad) # pad in inch
+            self.append_size(d, padded_size)
+
+
 class AxesLocator(object):
     """
     A simple callable object, initiallized with AxesDivider class,
@@ -836,7 +862,18 @@
 
     return divider
 
+def make_axes_area_auto_adjustable(ax, 
+                                   use_axes=None, pad=0.1,
+                                   adjust_dirs=["left", "right", "bottom", 
"top"]):
+    
+    divider = make_axes_locatable(ax)
 
+    if use_axes is None:
+        use_axes = ax
+
+    divider.add_auto_adjustable_area(use_axes=use_axes, pad=pad,
+                                     adjust_dirs=adjust_dirs)
+
 #from matplotlib.axes import Axes
 from mpl_axes import Axes
 LocatableAxes = locatable_axes_factory(Axes)

Modified: trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_size.py
===================================================================
--- trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_size.py   2010-12-29 
08:29:11 UTC (rev 8851)
+++ trunk/matplotlib/lib/mpl_toolkits/axes_grid1/axes_size.py   2010-12-29 
08:29:59 UTC (rev 8852)
@@ -12,8 +12,8 @@
 """
 
 import matplotlib.cbook as cbook
+from matplotlib.axes import Axes
 
-
 class _Base(object):
     "Base class"
 
@@ -238,3 +238,54 @@
     raise ValueError("Unknown format")
 
 
+class SizeFromFunc(_Base):
+    def __init__(self, func):
+        self._func = func
+
+    def get_size(self, renderer):
+        rel_size = 0.
+
+        bb = self._func(renderer)
+        dpi = renderer.points_to_pixels(72.)
+        abs_size = bb/dpi
+
+        return rel_size, abs_size
+
+class GetExtentHelper(object):
+    def _get_left(tight_bbox, axes_bbox):
+        return axes_bbox.xmin - tight_bbox.xmin
+
+    def _get_right(tight_bbox, axes_bbox):
+        return tight_bbox.xmax - axes_bbox.xmax
+
+    def _get_bottom(tight_bbox, axes_bbox):
+        return axes_bbox.ymin - tight_bbox.ymin
+
+    def _get_top(tight_bbox, axes_bbox):
+        return tight_bbox.ymax - axes_bbox.ymax
+
+    _get_func_map = dict(left=_get_left,
+                         right=_get_right,
+                         bottom=_get_bottom,
+                         top=_get_top)
+
+    del _get_left, _get_right, _get_bottom, _get_top
+    
+    def __init__(self, ax, direction):
+        if isinstance(ax, Axes):
+            self._ax_list = [ax]
+        else:
+            self._ax_list = ax
+            
+        try:
+            self._get_func = self._get_func_map[direction]
+        except KeyError:
+            print "direction must be one of left, right, bottom, top"
+            raise
+
+    def __call__(self, renderer):
+        vl = [self._get_func(ax.get_tightbbox(renderer),
+                             ax.bbox) for ax in self._ax_list]
+        return max(vl)
+    
+


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

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to