Ticket #5128: trac_5128-matplotlib-plot.patch

File trac_5128-matplotlib-plot.patch, 4.5 KB (added by jason, 13 years ago)
  • sage/plot/all.py

    # HG changeset patch
    # User Jason Grout <jason-sage@creativetrax.com>
    # Date 1233870931 21600
    # Node ID 3219f0a0083d1038b5ad2b91881789994dddb105
    # Parent  f0749952a6116284bd9636c8eecf2f19f71b2aee
    [mq]: matplotlib_plot
    
    diff -r f0749952a611 -r 3219f0a0083d sage/plot/all.py
    a b  
    1919from circle import circle
    2020from contour_plot import contour_plot, implicit_plot, region_plot
    2121from density_plot import density_plot
     22from matplotlib_plot import matplotlib_plot
    2223
    2324from animate import Animation as animate
    2425
  • new file sage/plot/matplotlib_plot.py

    diff -r f0749952a611 -r 3219f0a0083d 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
  • sage/plot/plot.py

    diff -r f0749952a611 -r 3219f0a0083d sage/plot/plot.py
    a b  
    2626    \item plot_vector_field
    2727    \item matrix_plot
    2828    \item graphics_array
     29    \item matplotlib_plot
    2930\end{itemize}
    3031
    3132The following miscellaneous Graphics functions are included: