Ticket #5128: trac_5128-matplotlib-plot.2.patch

File trac_5128-matplotlib-plot.2.patch, 3.8 KB (added by jason, 12 years ago)

apply instead of previous patch.

  • new file sage/plot/matplotlib_plot.py

    # HG changeset patch
    # User Jason Grout <jason-sage@creativetrax.com>
    # Date 1233870931 21600
    # Node ID a32c8189ea163434ca602101976640ab713e4fa5
    # Parent  3b1e17c09ce5172481efd9b24fc8921fcf33ab1b
    [mq]: matplotlib_plot
    
    diff -r 3b1e17c09ce5 -r a32c8189ea16 sage/plot/matplotlib_plot.py
    - +  
     1r"""
     2Implements a wrapper class for Matplotlib artists to be integrated in
     3with the Sage graphics system.  See the documentation for matplotlib_plot.
     4"""
     5
     6#*****************************************************************************
     7#       Copyright (C) 2009 Jason Grout <jason-sage@creativetrax.com>
     8#
     9#  Distributed under the terms of the GNU General Public License (GPL)
     10#
     11#    This code is distributed in the hope that it will be useful,
     12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14#    General Public License for more details.
     15#
     16#  The full text of the GPL is available at:
     17#
     18#                  http://www.gnu.org/licenses/
     19#*****************************************************************************
     20
     21from sage.plot.primitive import GraphicPrimitive
     22from sage.plot.plot import  Graphics
     23
     24class Matplotlib_Primitive(GraphicPrimitive):
     25    """
     26    Primitive class that wraps a Matplotlib artist
     27    """
     28    def __init__(self, axes, options=None):
     29        """
     30        sage: from pylab import randn, hist
     31        sage: g = hist(randn(1000), 100)
     32        sage: gg=Matplotlib_Primitive(g)
     33        sage: type(gg)
     34        """
     35        self.axes = axes
     36        GraphicPrimitive.__init__(self, options)       
     37
     38    def get_minmax_data(self):
     39        """
     40        Returns a dictionary with the bounding box data.
     41               
     42        EXAMPLES:
     43            sage: from pylab import imshow
     44            sage: A=random_matrix(RDF,100)
     45            sage: A.numpy()
     46            sage: B=A.numpy().astype(float)
     47            sage: im = imshow(B/numpy.max(B),  origin='upper',alpha=0.6)
     48            sage: print Matplotlib_Primitive(im)
     49
     50            sage: m = matrix_plot(matrix([[1,3,5,1],[2,4,5,6],[1,3,5,7]]))[0]
     51            sage: list(sorted(m.get_minmax_data().items()))
     52            [('xmax', 4), ('xmin', 0), ('ymax', 3), ('ymin', 0)]
     53
     54        """
     55        d = {}
     56        axes = self.axes.get_axes()
     57        if not isinstance(axes, list):
     58            axes = [axes]           
     59        xlims = [axis.get_xbound() for axis in axes]
     60        ylims = [axis.get_ybound() for axis in axes]
     61        d['xmin'] = min(i[0] for i in xlims)
     62        d['xmax'] = max(i[1] for i in xlims)
     63        d['ymin'] = min(i[0] for i in ylims)
     64        d['ymax'] = max(i[1] for i in ylims)
     65        return d
     66
     67
     68    def _allowed_options(self):
     69        """
     70        No options are allowed.
     71
     72        EXAMPLE
     73            sage: from pylab import randn, hist
     74            sage: g = hist(randn(1000), 100)
     75            sage: gg = Matplotlib_Primitive(g)
     76            sage: gg._allowed_options()
     77
     78        """
     79        return {}
     80
     81    def _repr_(self):
     82        """
     83       
     84        """
     85        return "MatplotlibPlotPlot defined by a %s x %s data grid"%(self.xy_array_row, self.xy_array_col)
     86
     87    def _render_on_subplot(self, subplot):
     88        """
     89        Draw a matplotlib artist onto another artist
     90        """
     91        print type(subplot)
     92        subplot.add_artist(self.axes)
     93
     94def matplotlib_plot(mat):
     95    """
     96    Wraps a matplotlib artist in a Sage Graphics object.
     97
     98    EXAMPLE
     99        sage: from pylab import randn, hist
     100        sage: g = hist(randn(1000), 100)
     101        sage: matplotlib_plot(g)
     102    """
     103    g = Graphics()
     104    g.add_primitive(Matplotlib_Primitive(mat))
     105    return g